Python读取not blank.txt文件的结果为空行
我有以下代码:Python读取not blank.txt文件的结果为空行,python,Python,我有以下代码: outtxt = "output2.txt" command = f"ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 {inputpath}" with open(outtxt, "w") as output: subprocess.Popen(co
outtxt = "output2.txt"
command = f"ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 {inputpath}"
with open(outtxt, "w") as output:
subprocess.Popen(command, stdout=output, stderr=output, text=True, creationflags=0x08000000)
output.close()
with open(outtxt, "r") as file:
data = file.read()
print(data)
当我运行这个程序时,Python只打印一个空行。(“”)但肯定有这样一个文件,它有一行内容存储视频文件的帧数<代码>813但python无法读取该行。如何读取帧计数并转换为整数?(我在Windows 10上)
编辑:抱歉缩进。我修复了缩进并编写了完整的代码。另外,我试图关闭第一个文件,但也没有成功。我检查了output2.txt,它不是空的。我已删除并重试,但它不是空的。Popen()
只是启动了一个进程。您将需要.communication()
和/或.wait()
进行通信。。。或者,更好的方法是使用subprocess.run()
,它代表您处理这些细节
outtxt=“output2.txt”
command=f“ffprobe-v error-select_streams v:0-show_entries stream=nb_frames-of default=nokey=1:noprint_wrappers=1{inputpath}”
以open(OUTXT,“w”)作为输出:
subprocess.run(命令,stdout=output,stderr=output,text=True,creationflags=0x08000000)
如注释中所述,
with
语句负责为您关闭文件,因此无需显式地.close()
它。能否显示文件内容和命令
?请发布,修复代码段的缩进,什么是命令
,等等。压痕是否正确?如果是这样,则在同一文件已打开时打开该文件。这可能是个问题。当您将与
一起使用时,不需要输出.close()
。请注意,您是否确实检查了output2.txt
的内容?