这是邪恶的:
p = subprocess.Popen('unrar e ' + root + '/' + i + ' ' + testfolder,
bufsize=2048, shell=True, stdin=subprocess.PIPE)
代替,
p = subprocess.Popen(['unrar', 'e', '%s/%s' % (root, i), testfolder],
bufsize=2048, stdin=subprocess.PIPE)
p.stdin.write('e')
p.wait()
if p.returncode == 0:
pass # put code that must only run if successful here.
通过将精确数组而不是字符串传递给Popen而不使用shell = True,其中包含空格的文件名不能被解释为多个参数,或子shell命令,或其他一些潜在的恶意内容(想想其名称中包含$(rm -rf ..)的文件).
然后,在调用p.wait()之后(当你没有捕获stderr或stdout时不需要p.communicate()),你必须检查p.returncode以确定进程是否成功,并且只继续删除文件,如果p.returncode == 0(表示成功).
在unrar进程仍在运行时,p.communicate()返回的初始诊断是不可行的; p.communicate()和p.wait()不会那样工作.
如果在ssh上运行,则会稍微改变一下:
import pipes # in Python 2.x; in 3.x, use shlex.quote() instead
p = subprocess.Popen(['ssh', ' '.join(
[pipes.quote(s) for s in ['unrar', 'e', '%s/%s' % (root, i), testfolder]])