处理带有空格的目录 Python subprocess.call() [英] Handling directories with spaces Python subprocess.call()

查看:50
本文介绍了处理带有空格的目录 Python subprocess.call()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个扫描文本文件并将参数传递给子进程的程序.一切正常,直到我得到路径中有空格的目录.

I'm trying to create a program that scans a text file and passes arguments to subprocess. Everything works fine until I get directories with spaces in the path.

我的 split 方法,它分解参数在空格上跳:

My split method, which breaks down the arguments trips up over the spaces:

s = "svn move folder/hello world anotherfolder/hello world"

task = s.split(" ")
process = subprocess.check_call(task, shell = False)

做,要么我需要函数来解析正确的参数,要么我将整个字符串传递给子进程而不先分解它.

Do, either I need function to parse the correct arguments, or I pass the whole string to the subprocess without breaking it down first.

不过我有点迷茫.

推荐答案

改用列表:

task = ["svn",  "move",  "folder/hello world", "anotherfolder/hello world"]
subprocess.check_call(task)

如果您的文件包含整个命令,而不仅仅是路径,那么您可以尝试 shlex.split():

If your file contains whole commands, not just paths then you could try shlex.split():

task = shlex.split(s)
subprocess.check_call(task)

这篇关于处理带有空格的目录 Python subprocess.call()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆