1

I'm trying to open a file with spaces in path with subprocess.call function but I can't get it working.

import subprocess subprocess.call(['cmd','/c','start C:/Users/akg/Desktop/file 1.png']) 

I've tried also

import subprocess file= '"C:/Users/akg/Desktop/file 1.png"' subprocess.call(['cmd','/c','start '+file]) 

But I still getting this error
1st case : 1st case screenshot, 2nd case 2nd case : screenshot even spliting the command whenever there is a whitespace is not working

2

2 Answers 2

1

You should put every argument as a separate item in the list passed to subprocess.call:

subprocess.call(['cmd','/c','start', 'C:/Users/akg/Desktop/file 1.png']) 
Sign up to request clarification or add additional context in comments.

3 Comments

@PeterWood True. Updated as suggested. Thanks.
See also list2cmdline
When i split the command the cmd opens without doing anything. Thanks for the reply
0

The solution that worked for me is to delete the start option as described here : Opening file with spaces in Windows via Command Prompt

import subprocess subprocess.call(['cmd','/c',"C:/Users/akg/Desktop/file 1.png"]) 

or

subprocess.call(['cmd', '/c', 'start', "", "C:/Users/akg/Desktop/file 1.png"]) 

Thank you all

1 Comment

with a small change it works subprocess.call(['cmd', '/c', 'start', "", "C:/Users/akg/Desktop/file 1.png"])

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.