Quote spaces in filename when calling subprocess

家住魔仙堡 提交于 2020-01-05 01:32:12

问题


Somebody came up with the brilliant idea of putting spaces in a filename. I need to do scp from python using that filename, which is problematic because the shell parses the command, and scp has also some quircks regarding spaces. This is my test code:

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

Which gives:

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

Using the naive version with command.split(" "):

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

The right, working scp command would be:

['scp', 'localfile.txt', 'hostname:"/some/directory/a file with spaces.txt"']
  1. Is there a ready solution for this?
  2. If not, what would be the robust way of doing:
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"']

回答1:


command = split_it('scp {} {}:"{}"'.format(localfile, host, mypath))
  1. Instead of building a command string, only to split_it again, directly build a list of arguments.

  2. In order to add one layer of quoting to the remote file path, use shlex.quote (or pipes.quote if using older Python versions).

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

Sources/related posts:

  • How to escape spaces in path during scp copy in linux?
  • Python scp copy file with spaces in filename
  • https://docs.python.org/3/library/subprocess.html#popen-constructor


来源:https://stackoverflow.com/questions/44596145/quote-spaces-in-filename-when-calling-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!