Python - 在subprocess.Popen命令中使用变量

2 投票
3 回答
6008 浏览
提问于 2025-04-18 02:37

我刚开始学习编程,需要一些帮助。我正在写一个Python脚本,这个脚本会遍历一个文件夹里的内容,并在遍历过程中把每个文件发送到一个蓝牙设备。

如果我直接指定文件名,它工作得很好,但我用变量来表示文件名时就不行了。下面是我的代码:

import os
import time
import subprocess

indir = '\\\\10.12.12.218\\myshare'
for  root, dirs, filenames in os.walk(indir):
   for file in filenames:
      print (file)
      subprocess.Popen('ussp-push /dev/rfcomm0 image1.jpg file.jpg', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print ('end') 

我想把命令中的'image1.jpg'替换成变量'file',像下面这样,但一直没有成功。

subprocess.Popen('ussp-push /dev/rfcomm0', file, 'file.jpg', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

非常感谢任何帮助。

3 个回答

-1

所以,你有一堆文件,想要对每个文件执行一次命令。我觉得下面的代码应该能很好地解决这个问题。

import os
import time
import subprocess

indir = '\\\\10.12.12.218\\myshare'
for  root, dirs, filenames in os.walk(indir):
    for file in filenames:
        print 'Sending', os.path.join(root, file)
        subprocess.check_call(['ussp-push', '/dev/rfcomm0', os.path.join(root, file), 'file.jpg'])
print ('end')

这是我做的一些修改:

  1. 我用 check_call 函数替代了 Popen,因为你想要顺序执行命令,而不是并行执行。check_call 函数的参数和 Popen 一样,但它会等命令执行完毕,如果执行失败还会抛出异常。
  2. 我把一个数组(用方括号写的)作为第一个参数传给 check_call,这个数组里包含了要执行的命令。这也意味着不需要用 shell 来解释命令字符串,所以我去掉了 shell=True。这个数组的第一个元素是命令,后面的元素是传给它的参数。
  3. 数组里的最后一个元素是文件的完整路径。file 变量只保存了文件名,但我们需要它的路径,因为文件可能在某个文件夹的深处(因为你在 walk 遍历文件夹)。os.path.join 会把两个字符串用合适的 \/ 连接起来,具体取决于你使用的操作系统。
  4. 我还去掉了 stdoutstderr 参数。这意味着命令的输出和错误信息会直接显示在命令行上,这可能正是你想要的。stdoutstderr 参数在你想要读取命令输出并自己处理时才有意义,而不是直接在终端显示。
1

试试这个:

subprocess.Popen(
    ['ussp-push', '/dev/rfcomm0', file, 'file.jpg'],
     stdout=subprocess.PIPE, 
     stderr=subprocess.PIPE)

你需要给 Popen() 传递一个字符串列表。另一种方法是构建一个用空格分开的命令,比如:

subprocess.Popen(
    'ussp-push /dev/rfcomm0 "{0}" file.jpg'.format(file) # replace {0} with file
     stdout=subprocess.PIPE, 
     stderr=subprocess.PIPE)

使用 shell=True 安全么?

我想说几点关于使用 shell=True 的事情。

  1. 在这种情况下,正如 m.wasowski 在评论中提到的,这并不是必要的。
  2. 如果你对命令没有控制权,使用 shell=True 是不安全的。比如,如果你从用户输入中获取命令,用户可能会输入像 sudo rm -fr / 这样的命令。
  3. 这不安全的原因是,一旦调用了 shell,PATH 可能会不同。当你发出像 ls 这样的命令时,它可能不是来自通常的位置(/bin/ls),而是来自一些恶意的位置,比如 /home/evil/bin

不过,如果你对命令有控制权,使用 shell=True 就是安全的。在这种情况下,/dev/rfcomm0 是你定义的命令,而不是从其他地方接收的。感谢 m.wasowski 提出这个观点。

更新

去掉 shell=True。请查看评论。

3

这里有几个问题:

  • shell=True 是多余的。去掉它,改用列表作为参数:

    import shlex
    
    args = shlex.split('ussp-push /dev/rfcomm0 image1.jpg file.jpg')
    
  • 你试图把命令行参数当作单独的参数传给 Popen。应该用 Popen(['echo', 'a']),而不是 Popen('echo', 'a')。后者完全是错误的。可以查看Popen() 函数的文档了解更多信息。

  • 除非你要从 p.stdoutp.stderr 中读取数据,否则不要使用 stdout=PIPE 和/或 stderr=PIPE,否则如果子进程填满了操作系统的管道缓冲区,它可能会一直阻塞。

  • 保存 Popen() 的引用,以便后面可以等待它的状态。这是可选的,但可以帮助避免产生太多的僵尸进程。

你可以把生成文件的部分提取到一个单独的函数中:

import os

def get_files(indir, extensions=('.jpg', '.png')):
    """Yield all files in `indir` with given `extensions` (case-insensitive)."""
    for root, dirs, files in os.walk(indir):
        for filename in files:
            if filename.casefold().endswith(extensions):
               yield os.path.join(root, filename)

然后可以并行执行每个文件的命令:

from subprocess import CalledProcessError, Popen

indir = r'\\10.12.12.218\myshare'
commands = [['ussp-push', '/dev/rfcomm0', path] for path in get_files(indir)]

# start all child processes
children = [Popen(cmd) for cmd in commands]

# wait for them to complete, raise an exception if any of subprocesses fail
for process, cmd in zip(children, commands):
    if process.wait() != 0:
       raise CalledProcessError(process.returncode, cmd)        

如果你不想并行运行命令,那就用 subprocess.call 替代 subprocess.Popen

import subprocess

indir = r'\\10.12.12.218\myshare'
statuses = [subprocess.call(['ussp-push', '/dev/rfcomm0', path])
            for path in get_files(indir)]
if any(statuses):
   print('some commands have failed')

这样会一次运行一个命令。

撰写回答