NoIdeaHowToFixThis
NoIdeaHowToFixThis

Reputation: 4574

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:

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. 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

Upvotes: 2

Views: 1810

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140307

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_ = ['qpdf-7.0.0/bin/qpdf','--password=%s'%'','--decrypt',target_file, target_file]
call(cmd_)

and leave the quoting to subprocess

As a side note, no need to double the slashes. It works, but that's unnecessary.

Upvotes: 3

Related Questions