Mos*_*teM 5 python string bash subprocess
我想调用像这样的命令
scp username@hostname:/dir/to/files/\{a,b,c\} /target/dir
Run Code Online (Sandbox Code Playgroud)
从 Python 用一个命令复制多个文件。如果直接输入 shell,该命令将完美运行。
但如果我使用
import subprocess
p = subprocess.Popen(['scp', 'username@hostname:/dir/to/files/\{a,b,c\}',
'/target/dir'])
sts = os.waitpid(p.pid, 0)
Run Code Online (Sandbox Code Playgroud)
我收到错误
scp: /dir/to/files/{a,b,c}: No such file or directory
Run Code Online (Sandbox Code Playgroud)
显然,反斜杠丢失了。如果我在 Popen 参数中使用双反斜杠,例如
\\{a,b,c,d\\}
Run Code Online (Sandbox Code Playgroud)
我收到错误
scp: /dir/to/target/\a: No such file or directory
scp: /dir/to/target/\b: No such file or directory
scp: /dir/to/target/\c\: No such file or directory
Run Code Online (Sandbox Code Playgroud)
如果我使用像这样的原始字符串,什么都不会改变r'\{' + r'\}'
如何使用正确转义的花括号从 Python 调用 scp 命令'\\{a,b,c\\}'?
让-弗朗索瓦·法布尔让我走上了正轨:
import subprocess
p = subprocess.Popen('scp username@hostname:/dir/to/files/\{a,b,c\} /target/dir',
shell=True)
sts = p.wait()
Run Code Online (Sandbox Code Playgroud)
这
shell=True
Run Code Online (Sandbox Code Playgroud)
争论是缺失的一点。这是一个不推荐的解决方案,但至少它是有效的。
| 归档时间: |
|
| 查看次数: |
7545 次 |
| 最近记录: |