Flush和fsync在tempfile中不起作用

时间:2013-06-28 17:58:05

标签: python temporary-files

我正在尝试创建一个文件。在这个文件中,我将所有需要处理的文件放在脚本中,然后将文件作为参数传递。我的问题是,有时列表不够长,无法填充缓冲区,也没有任何内容写入磁盘。我试图刷新和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

1 个答案:

答案 0 :(得分:2)

来自NamedTemporaryFile docstring

  

该名称是否可用于第二次打开文件,而   命名临时文件仍然是开放的,因平台而异(可以   所以在Unix上使用;它不能在Windows NT或更高版本上。)

因此,虽然您仍然打开文件,但可能无法从子进程中读取它。这就是我要做的事情:

fd, tmp_fpath = tempfile.mkstemp()
os.close(fd) # Needed on Windows if you want to access the file in another process

try:
    with open(tmp_fpath, "wb") as f:
        f.write("\n".join(file_list) + "\n")

    # ... subprocess stuff ...
    do_stuff_with(tmp_fpath)
finally:
    os.remove(tmp_fpath)