JCF
JCF

Reputation: 309

Using subprocess.Popen (shell=True) with windows folders

I'm currently looking at Popen to automate the compression & storage of documents.

For the compression part, I thought of the following Python line:

subprocess.Popen("WinRAR.exe a -r c:\\03. Notes\\AllTexts *.txt", shell=True)

I keep having error messages, as the command is not able to deal with a folder name that contains a space (03. Notes). This question was asked before several times, and, I must say that I tried all propositions: raw string, double quotes, triple quotes, ... None of them worked.

As I can't change the folder names, and I have no more ideas to try, could someone advise how could I possibly pass this command line successfully ?

Upvotes: 1

Views: 1409

Answers (1)

KromviellBlack
KromviellBlack

Reputation: 918

In Windows you should use quotes for file or directory names (if you want to use spaces inside). In Python, you should escape quotes with \ symbol (if you are using strings inside " quotes). Like this:

"my name is \"Mark\"!"

Or just:

'my name is "Mark"!'

So this will work as expected:

subprocess.Popen("WinRAR.exe a -r \"c:\\03. Notes\\AllTexts\" *.txt", shell=True)

As well as:

subprocess.Popen('WinRAR.exe a -r "c:\\03. Notes\\AllTexts" *.txt', shell=True)

Upvotes: 3

Related Questions