问题描述
我正在尝试使用此处的代码 https://stackoverflow.com/a/56454579 将文件上传到Windows 10上使用来自Python的WinSCP服务器.代码如下:
I am attempting to use the code from here https://stackoverflow.com/a/56454579 to upload files to a server with WinSCP from Python on Windows 10. The code looks like this:
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
命令不能正确处理文件名中的空格.我需要进行哪些更改才能使其正常工作?删除路径周围的双引号会给我一个不同的错误(它认为我正在尝试上传多个文件).
Which tells me that the put
command isn't properly handling the spaces in the filename. What do I need to change to get this to work? Removing the double quotes around the path gives me a different error (it thinks I'm trying to upload multiple files).
推荐答案
我认为您不能使用数组为WinSCP提供参数. subprocess.Popen
转义使用反斜杠在参数中使用双引号,则与转义双双引号发生冲突WinSCP.
I do not think you can use an array to provide arguments to WinSCP. The subprocess.Popen
escapes double quotes in the arguments using backslash, what conflicts with double double-quotes escaping expected by WinSCP.
您将必须自行格式化WinSCP命令行:
You will have to format the WinSCP command-line on your own:
args = "\"C:\Program Files (x86)\WinSCP\WinSCP.com\" /ini=nul " + \
f"/command \"open ftp://user:[email protected]\" \"put \"\"{path}\"\"\" exit"
(请注意,我已经省略了 option
命令,因为它们还是默认值)
(note that I've omitted your option
commands, as they are defaults anyway)
这篇关于执行WinSCP脚本时,subprocess.Popen中的Python双引号不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!