Reputation: 1651
When using subprocess.run
in Python, how do I execute a command where one of the arguments includes a space?
For example, I want to create a file with the name test file
(inc. space). In the shell you would do:
touch 'test file'
Using subprocess.run
in Python I have tried:
import subprocess
subprocess.run(["touch", "'test file'") # Creates a file called `'test file'` (inc. quotes)
subprocess.run(["touch", "test\ file") # Creates a file called `test\ me`
subprocess.run(["touch", "test", "file") # Creates two files `test` and `file`
subprocess.run(["touch", "'test", "file'") # Creates two files `'test` and `file'`
Upvotes: 1
Views: 1618
Reputation: 1651
No need to use quotes to specify an argument that includes a space as its treated as a string literal, just do:
subprocess.run(["touch", "test file"])
That creates a file called test file
.
Upvotes: 2