我正在制作一个小程序来获取您的系统的基本信息,这个程序在控制台中工作并输出信息,但是我希望它能够在C驱动器中创建一个文件夹,并在该文件夹中创建一个包含所有信息的txt文件。
当我运行程序时,它会创建文件夹和txt文件,但包含"None“。
def run_all_checks():
montior_cpu_times()
monitor_cpu_util()
monitor_cpu_cores()
monitor_cpu_freq()
monitor_RAM_Usage()
monitor_disk()
monitor_disk_usage()
monitor_network()
if not os.path.exists('C:\System Information Dump'):
os.makedirs('C:\System Information Dump')
save_path = 'C:\System Information Dump'
file_name = "System Info Dump.txt"
completeName = os.path.join(save_path, file_name)
print(completeName)
file1 = open(completeName, "a")
file1.write (str(run_all_checks()))
file1.close()
#def file1():
#return run_all_checks()
#info = file1()
#file = open("System Info Dump.txt","a")
#file.write(str(info))
#file.close()
#file1()注释的代码只是一个例子,说明我也尝试过,但没有奏效。
发布于 2021-10-10 14:13:31
你的run_all_checks不返回任何东西。如果您不确定函数列表的长度。您可以保留一个函数列表并对其进行迭代,并返回其值,甚至直接将该值写入文件。就像这样:
def run_all_checks():
return "testing"
def montior_cpu_times():
return "FUNC 1"
def montior_cpu_util():
return "FUNC 2"
def runtest2():
return "FUNC3"
funcs = [montior_cpu_times(), montior_cpu_util(), runtest2(), run_all_checks()]
for func in funcs:
print(func)发布于 2021-10-10 14:12:38
问题是run_all_checks()不返回任何东西。这就是为什么文本文件是空的。
例如,尝试:
def run_all_checks():
montior_cpu_times()
monitor_cpu_util()
monitor_cpu_cores()
monitor_cpu_freq()
monitor_RAM_Usage()
monitor_disk()
monitor_disk_usage()
monitor_network()
return "Checks ran successfully!"https://stackoverflow.com/questions/69515870
复制相似问题