Subprocess.Popen中的Python双引号在Windows 10上不起作用

jippyjoe4

我正在尝试使用此处的代码https://stackoverflow.com/a/56454579将文件从Windows 10上的Python用WinSCP上传到服务器。该代码如下所示:

import subprocess
path = r'C:\mp4s\Sci-Fi Light Flicker.mp4'

process = subprocess.Popen(
  ['C:\Program Files (x86)\WinSCP\WinSCP.com', '/ini=nul', '/command', 'option batch abort', 'option confirm off', 'open ftp://user:[email protected]', f'put "{path}"', 'exit'],
  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in iter(process.stdout.readline, b''):
  print(line.decode().rstrip())

我得到的输出:

batch           abort
confirm         off
Connecting to ftp.website.com ...
Connected
Starting the session...
Session started.
Active session: [1] [email protected]
File or folder '\C:\mp4s\Sci-Fi' does not exist.
System Error.  Code: 123.
The filename, directory name, or volume label syntax is incorrect
(A)bort, (R)etry, (S)kip, Ski(p) all: Abort

这告诉我“ put”命令不能正确处理文件名中的空格。我需要更改使其工作吗?删除路径周围的双引号会给我一个不同的错误(它认为我正在尝试上传多个文件)。

马丁·普里克里(Martin Prikryl)

我认为您不能使用数组为WinSCP提供参数。subprocess.Popen转义使用反斜杠在参数双引号,有什么冲突转义双双引号由WinSCP赋予的预期。

您将必须自行格式化WinSCP命令行:

args =  "\"C:\Program Files (x86)\WinSCP\WinSCP.com\" /ini=nul " + \
       f"/command \"open ftp://user:[email protected]\" \"put \"\"{path}\"\"\" exit"

(请注意,我已经省略了您的option命令,因为无论如何它们都是默认的)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章