在子进程sh中打开的文件

2024-06-01 09:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在执行以下脚本的最后,我会收到如下错误:

filename.enc: No such file or directory
140347508795048:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen('filename.enc','r')
140347508795048:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400: 

Popen似乎试图在执行结束时关闭该文件,尽管它已被删除。在

^{pr2}$

如何正确关闭文件?在

谢谢。在


Tags: or文件no脚本错误errorfilenamesystem
3条回答

听起来好像您的子进程在执行,但由于它是非阻塞的,因此os.remove(infile)会立即执行,在子进程完成之前删除文件。在

您可以改为使用^{},这将等待命令完成。在

。。。或者您可以更改代码以使用^{}

p = subprocess.Popen(opensslCmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
p.wait()
os.remove(infile)

你可以尝试运行子流程.Popen在一个线程中,并且只删除子进程调用返回的文件。在

参见:Python subprocess: callback when cmd exits

你也可以用这个。在

ss=subprocess.Popen(FileName,shell=True)
ss.communicate()

相关问题 更多 >