调用子进程时在文件名中引用空格

Quote spaces in filename when calling subprocess

有人想出了在文件名中加入空格的好主意。我需要使用该文件名从 python 执行 scp,这是有问题的,因为 shell 解析命令,并且 scp 也有一些关于空格的怪癖。这是我的测试代码:

import subprocess
import shlex


def split_it(command):
    return shlex.split(command)
    #return command.split(" ")


def upload_file(localfile, host, mypath):
    command = split_it('scp {} {}:"{}"'.format(localfile, host, mypath))
    print(command)
    res = subprocess.run(command, stdout=subprocess.PIPE)
    return res.stdout.decode()


upload_file("localfile.txt", "hostname", "/some/directory/a file with spaces.txt")

给出:

['scp', 'localfile.txt', 'hostname:/some/directory/a file with spaces.txt']
scp: ambiguous target

使用原始版本 command.split(" "):

['scp', 'localfile.txt', 'hostname:"/some/directory/a', 'file', 'with', 'spaces.txt"']
spaces.txt": No such file or directory

正确的 scp 命令应该是:

['scp', 'localfile.txt', 'hostname:"/some/directory/a file with spaces.txt"']
  1. 有现成的解决方案吗?
  2. 如果不是,那么稳健的做法是什么:
split_it('scp localfile.txt hostname:"/some/directory/a file with spaces.txt"')
# returns ['scp', 'localfile.txt', 'hostname:"/some/directory/a file with spaces.txt"']
command = split_it('scp {} {}:"{}"'.format(localfile, host, mypath))
  1. 不构建命令字符串,只是为了再次split_it,直接构建参数列表。

  2. 为了向远程文件路径添加一层引号,如果使用旧的 Python 版本,请使用 shlex.quote (or pipes.quote

command = ['scp', localfile, '{}:{}'.format(host, shlex.quote(mypath))]

Sources/related 个帖子: