我正在尝试创建一个文件。在这个文件中,我将所有需要处理的文件放在一个脚本中,并将文件作为参数传递。我的问题是,有时列表不够长,无法填满缓冲区,并且磁盘中没有写入任何内容。我试图刷新和 fsync 临时文件,但没有任何反应。该脚本是第三方脚本,因此我无法更改参数传递的方式。
with tempfile.NamedTemporaryFile(bufsize=0) as list_file:
list_file.write("\n".join(file_list) + "\n")
list_file.flush()
os.fsync(list_file)
command = "python " + os.path.join(SCRIPT_PATH, SCRIPT_NAME) + " --thread --thread_file "
ppss_command = [SCRIPT_PATH + "/ppss", "-f", list_file.name, "-c", command]
p = subprocess.Popen(ppss_command)
out,err = p.communicate()
最终解决方案代码(jterrace 答案):
with tempfile.NamedTemporaryFile(delete=False) as list_file:
list_file.write("\n".join(file_list) + "\n")
list_file.close()
command = "python " + os.path.join(SCRIPT_PATH, SCRIPT_NAME) + " --thread --thread_file "
ppss_command = [SCRIPT_PATH + "/ppss", "-f", list_file.name, "-c", command]
p = subprocess.Popen(ppss_command)
out, err = p.communicate()
list_file.delete = True