python等待shell命令完成 [英] python to wait for shell command to complete

查看:62
本文介绍了python等待shell命令完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行脚本来解压缩一些文件,然后删除 rar 文件.我是通过 shell 运行命令来做到这一点的.我尝试了几种不同的方法来让脚本等到文件解包完成,但它仍然会继续并在文件使用完成之前删除文件.

I am running script to unrar some files and the remove the rar files afterwards. I am doing this by running the command through shell. I have tried several different ways to make the script wait until it's done unpacking the files, but it still goes ahead and deletes the file before they are done being used.

我已经尝试了下面的代码,这是行不通的.我试图看看我是否可以让 wait() 工作,但也没有运气.

I have tried the code below, which is a no go. I have tried to see if i could get the wait() to work, but also no luck.

有什么想法吗?运行 python 2.7

Any ideas? running python 2.7

我希望脚本运行命令:)

I want the script to run the command :)

            p = subprocess.Popen('unrar e ' + root + '/' + i + ' ' + testfolder,
                                 bufsize=2048, shell=True,
                                 stdin=subprocess.PIPE)
            p.stdin.write('e')
            p.communicate()

for root, dirs, files in os.walk(testfolder):
    for i in files:

        print 'Deleting rar files'
        os.remove(i)

for i in os.listdir(testfolder):
    if os.path.isdir(testfolder + i):
        shutil.rmtree(testfolder + i)

推荐答案

这太邪恶了:

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 ..) 的文件).

By passing an exact array rather than a string to Popen and not using shell=True, a filename with a space in it can't be interpreted as a more than one arguments, or a subshell command, or some other potentially malicious thing (think of a file with $(rm -rf ..) in its name).

然后,在调用 p.wait() 之后(当您不捕获 stderr 或 stdout 时,不需要 p.communicate()),您必须检查p.returncode 判断进程是否成功,只有在p.returncode == 0(表示成功)时才继续删除文件.

Then, after calling p.wait() (there's no need for p.communicate() when you aren't capturing stderr or stdout), you must check p.returncode to determine whether the process was successful, and only proceed on to delete files if p.returncode == 0 (indicating success).

您的初步诊断,即 p.communicate()unrar 进程仍在运行时返回,是不可行的;p.communicate()p.wait() 不能那样工作.

Your initial diagnosis, that p.communicate() is returning while the unrar process is still running, is not feasible; p.communicate() and p.wait() do not work that way.

如果通过 ssh 运行,这会有所改变:

If running across ssh, this changes a bit:

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]])

这篇关于python等待shell命令完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆