简体   繁体   English

带有空间的路径上的subprocess.call

[英]subprocess.call on path with space

the string that contains a file looks like this in the console: 包含文件的字符串在控制台中如下所示:

>>> target_file
'src//data//annual_filings//ABB Ltd//ABB_ar_2015.pdf'

I got the target_file from a call to os.walk The goal is to build a command to run in subprocess.call Something like: 我从对os.walk的调用中获得了target_file 。目标是建立一个在subprocess.call运行的命令。

from subprocess import call

cmd_ = r'qpdf-7.0.0/bin/qpdf --password=%s --decrypt %s %s' %('', target_file, target_file)
call([cmd_])

I tried different variations, setting shell to either True or False . 我尝试了其他变体,将shell设置为TrueFalse Replacing the // with / , \\ etc. The issue seems to be with the space in the folder (I can not change the folder name). /\\等替换// 。问题似乎出在文件夹中的空格上(我无法更改文件夹名称)。

The python code needs to run on Windows python代码需要在Windows上运行

you have to define cmd_ as a list of arguments not a list with a sole string in it, or subprocess interprets the string as the command (doesn't even try to split the args): 您必须将cmd_定义为参数列表,而不是其中包含唯一字符串的列表,否则subprocess cmd_字符串解释为命令(甚至不尝试拆分args):

cmd_ = ['qpdf-7.0.0/bin/qpdf','--password=%s'%'','--decrypt',target_file, target_file]
call(cmd_)

and leave the quoting to subprocess 并将报价留给subprocess

As a side note, no need to double the slashes. 附带说明,无需将斜杠加倍。 It works, but that's unnecessary. 它有效,但这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM