Skip to main content
 首页 » 编程设计

如果使用 subprocess.Popen 运行另一个脚本,Python 会两次打开一个文件

2024年10月17日7zhengyun_ustc

我有脚本test1.py(例如)

import test2 
 
 
def main(): 
    f = open("File.txt") 
    test2.run() 
 
    while True: 
        pass 
 
if __name__ == '__main__': 
    main() 

和test2.py

import subprocess, sys, os 
 
def run(): 
    # Self run 
    subprocess.Popen(['C:\\Windows\\System32\\cmd.exe', '/C', 'start', 
                          sys.executable, os.path.abspath(__file__)]) 
 
def main(): 
    while True: 
        pass 
 
if __name__ == '__main__': 
    main() 

问题是第二个脚本的启动重新打开文件“File.txt”。为什么第二个脚本在启动时打开文件?

D:\test>Handle.exe File.txt 
 
Handle v4.0 
Copyright (C) 1997-2014 Mark Russinovich 
Sysinternals - www.sysinternals.com 
 
python.exe         pid: 9376   type: File           29C: D:\test\File.txt 
python.exe         pid: 9792   type: File           29C: D:\test\File.txt 

请您参考如下方法:

子进程继承一个文件句柄以与 Unix 模型兼容(更多解释参见 PEP 446)。在子流程调用之后,原始脚本和新脚本都应该可以访问该文件;在 Windows 上,这意味着打开两个单独的文件句柄。

如果你想避免这种情况,一个更好的习惯用法是在 fork 之前关闭文件,明确地或使用 with 结构:

with open('File.txt', 'r') as inputfile: 
    pass # or do something with inputfile