如何在不写入2次的情况下将数据写入.txt文件

时间:2018-10-18 11:45:26

标签: python python-3.x

我正在尝试将一些数据写入txt文件。但是每次它将数据写入两次,而我只希望一次。无法弄清楚..

数据是从计算机收集的,它是cpu,gpu,硬盘驱动器等。我正在收集。

任何帮助都可以申请:)

下面我附上了我使用的模块以及如何在两个列表中收集数据,并将两个列表写入同一文件。

我试图在一个列表中完成所有操作,但是后来我无法收集两个GPU。使用GPU是唯一使用循环收集数据的地方。

import platform                         #   OTHER INFORMATION
from psutil import virtual_memory       #   RAM
import cpuinfo                          #   CPU
from gpuinfo.windows import get_gpus    #   GPU
import psutil                           #   HD

# ----------------------------------------------------------------------------------------------------------------------
content_list = [a, b, c, d, e, g, h, i, j, k, l, m, n]


with open(dataFile, "a") as f:
    for item in content_list:
        f.write("%s\n" % item)


for gpu in get_gpus():
    f = (str("Graphic Card informaion = ") + str(gpu.__dict__))


    gpucontent_list = [f]


    with open(dataFile, "a") as f: 
        for item in gpucontent_list:
            f.write("%s\n" % item)

dataFile的输出看起来像这样,其中所有内容都出现两次。

Machine platform = AMD64
Windows version = 10.0.16299
The platform archictecture = ('32bit', 'WindowsPE')
Ramsize = 31.897907257080078 GB
Processor information = Intel(R) Xeon(R) CPU E3-1535M v5 @ 2.90GHz
Total C disk size = 227.00927352905273
Total C disk usage = 217.63811874389648
Free space on C disk = 9.37115478515625
Percent in use on C disk = 95.9
Total D disk size = 465.63573837280273
Total D disk usage = 52.99754333496094
Free space on D disk = 412.6381950378418
Percent in use on D disk = 11.4
Graphic Card informaion = {'name': 'NVIDIA Quadro M2000M', 'total_memory': 4095, 'device_id': 'VideoController1'}
Graphic Card informaion = {'name': 'Intel(R) HD Graphics P530', 'total_memory': 1024, 'device_id': 'VideoController2'}
Machine platform = AMD64
Windows version = 10.0.16299
The platform archictecture = ('32bit', 'WindowsPE')
Ramsize = 31.897907257080078 GB
Processor information = Intel(R) Xeon(R) CPU E3-1535M v5 @ 2.90GHz
Total C disk size = 227.00927352905273
Total C disk usage = 217.63818359375
Free space on C disk = 9.371089935302734
Percent in use on C disk = 95.9
Total D disk size = 465.63573837280273
Total D disk usage = 52.99754333496094
Free space on D disk = 412.6381950378418
Percent in use on D disk = 11.4
Graphic Card informaion = {'name': 'NVIDIA Quadro M2000M', 'total_memory': 4095, 'device_id': 'VideoController1'}
Graphic Card informaion = {'name': 'Intel(R) HD Graphics P530', 'total_memory': 1024, 'device_id': 'VideoController2'}

整个代码如下:

import platform                         #   OTHER INFORMATION
from psutil import virtual_memory       #   RAM
import cpuinfo                          #   CPU
from gpuinfo.windows import get_gpus    #   GPU
import psutil

dataFile = "pcinfo.txt"

machInfo = platform.machine()
versInfo = platform.version()
platArcInfo = platform.architecture()

# Gather Ram information
mem = virtual_memory()
RamVar = mem.total  # total physical memory available

# Gather the processor informaiton
cpuInfo = cpuinfo.get_cpu_info()['brand']

# Gather HD information
obj_DiskC = psutil.disk_usage('C:/')
obj_DiskD = psutil.disk_usage('D:/')

# ----------------------------------------------------------------------------------------------------------------------


a = (str("Machine platform = ") + machInfo)
b = (str("Windows version = ") + versInfo)
c = (str("The platform archictecture = ") + str(platArcInfo))

d = (str("Ramsize = ") + str(RamVar / (1024.0 ** 3)) + str(" GB"))

e = (str("Processor information = ") + cpuInfo)

# C - drive
g = (str("Total C disk size = ")+ str(obj_DiskC.total / (1024.0 ** 3)))
h = (str("Total C disk usage = ") + str(obj_DiskC.used / (1024.0 ** 3)))
i = (str("Free space on C disk = ") + str(obj_DiskC.free / (1024.0 ** 3)))
j = (str("Percent in use on C disk = ") + str(obj_DiskC.percent))
# D - drive
k = (str("Total D disk size = ")+ str(obj_DiskD.total / (1024.0 ** 3)))
l = (str("Total D disk usage = ") + str(obj_DiskD.used / (1024.0 ** 3)))
m = (str("Free space on D disk = ") + str(obj_DiskD.free / (1024.0 ** 3)))
n = (str("Percent in use on D disk = ") + str(obj_DiskD.percent))

# ----------------------------------------------------------------------------------------------------------------------
content_list = [a, b, c, d, e, g, h, i, j, k, l, m, n]


with open(dataFile, "a") as f: # Append the list data entry to the txt file
    for item in content_list:
        f.write("%s\n" % item)


for gpu in get_gpus():
    f = (str("Graphic Card informaion = ") + str(gpu.__dict__))


    gpucontent_list = [f]


    with open(dataFile, "a") as f: # Append the list data entry to the txt file
        for item in gpucontent_list:
            f.write("%s\n" % item)

1 个答案:

答案 0 :(得分:0)

以“ a”模式打开文件时:

with open(dataFile, "a") as f:

您将以追加模式打开文件,这意味着所有新写入都将写入文件的末尾,而不是替换文件的内容。

您希望以“ w”模式打开文件,这将截断现有内容,并在程序的其余部分保持打开同一文件对象,或者仅对以后的重新打开使用附加模式。

with open(dataFile, "w") as f:
    for item in content_list:
        f.write("%s\n" % item)

    for gpu in get_gpus():
        for item in gpucontent_list:
            f.write("%s\n" % item)