python-3.x - 如何每次将命令行文件保存为 .txt 文件。在 CMD 中需要显示 speedtest 输出,将这些命令行保存为 .txt 文件
问题描述
这是下面给出的程序
导入操作系统导入时间
- 索引=0
- file=open("out_{index}.txt", 'a')
- 而真:
- ps=os.system(f"speedtest.exe")
- 文件附加(ps)
- 索引+=1
- 时间.sleep(4)
解决方案
我相信它是 file.write() 而不是 file.append()。
我认为您每次都想写入一个新文件。
我会这样做。
import time
import os
index = 0
while True:
ps = os.system("speedtest.exe")
with open(f"out_{index}.txt", 'w+') as f:
f.write(ps)
index += 1
time.sleep(4)
但是,如果你想写在同一个文件中。
我会这样做。
import time
import os
with open("out_file.txt", 'a+') as f:
index = 0
while True:
ps = os.system("speedtest.exe")
f.write(ps)
index += 1
time.sleep(4)
推荐阅读
- go - for 循环中的短格式 var 声明
- oop - 继承实现的设计问题
- adobe-embed-api - 是否可以在 adobe embed api 中创建自定义注释
- ios - 在 iOS 上,节点服务器的会话 cookie 被初始化
- javascript - 当可见数据点全为零时,您可以隐藏 amCharts 图表中的图形吗?
- c# - 我有一个使用 c# 创建的 Azure Web 函数。并出现函数应用超时和主机关闭 4 分钟的间歇性问题
- python - 匹配数据名中具有相同位数的两列中的值
- python - 在匹配中将数组的值加倍 - Python
- arm - 在 macOS Intel 上生成和运行 ARMv8 SVE 二进制文件
- r - 结合5个ggplots