开发者

Files opened in subprocess shell

At the end of the execution of the following script, I receive some errors like these:

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: 

It seems like Popen is trying to close the file at the end of execution, although it was removed.

#!/usr/bin/python
import subprocess, os

infile = "filename.enc"
outfile开发者_开发知识库 = "filename.dec"
opensslCmd = "openssl enc -a -d -aes-256-cbc -in %s -out %s" % (infile, outfile)   
subprocess.Popen(opensslCmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
os.remove(infile)

How can I close the file properly?

Thanks.


it sounds like your subprocess executes, but since it is non-blocking, your os.remove(infile) executes immediately after, deleting the file before the subprocess finishes.

you could use subprocess.call() instead, which will wait for the command to finish.

... or you could change your code to use wait():

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


You could try running the subprocess.Popen in a thread, and just deleting the file after the subprocess call returned.

see: Python subprocess: callback when cmd exits


You can also use this.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜