Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subprocess.run does not work on Windows with file names containing spaces

I am using Powershell in Windows 10

>>> import os, subprocess
>>> os.listdir('C:\\Program Files') # works
>>> subprocess.run('ls C:\\') # works
>>> subprocess.run('ls C:\\Program Files') # Fails

I have tried forward slashes, escaping, ‘r’ to make a regex, the string enclosed within quotes, and nothing seems to work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That won’t work in the terminal either. You should quote the file path in the subprocess command:

subprocess.run('ls "C:\\Program Files"')

Everything in those quotes is then read as a single term.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading