March 7th, 2013, 08:51 AM
Subprocess - spaces in path
I have the following code:
s = "c:\\Users"
This doesn't run:
s = "dir c:\\Users\\My Name" # space between My and Name
It's the space between My and Name.
I've googled and tried several diff approaches but none work.
Also, for any command to work I have to set shell=True. If I don't do this nothing runs in subprocess.
I'm using Py2.7.3.
March 7th, 2013, 09:18 AM
try: s="dir c:\\Users\\My"+chr(32)+"Name"
March 7th, 2013, 09:26 AM
That didn't work.
Originally Posted by rrashkin
March 7th, 2013, 09:40 AM
If you want to have arguments in check_output(), you need to present it as a list.
Originally Posted by jackmanjls
This is not tested on windows, but this works for me:
Also, please describe what errors you get, "this runs" and "this doesn't run" are not very helpful error descriptions.
# I have this file structure
a folder/a file
par@partoj ~$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> output = subprocess.check_output(['ls', 'a folder'])
>>> print output
March 7th, 2013, 11:17 AM
Anyway, rashkin had the right idea (I'm not a regular ms dos user, so this is untested), just misquoted.
os.listdir(r'"c:\Documents and Settings\My account\and a subdirectory"')
command = r'dir "c:\Documents and Settings"'
The point is you need to pass the double quotes through to the dos command.
It's the same as if you'd typed the dir command at the
Maybe partoj's solution fails because
Originally Posted by wikipedia
[/code] are essential for python code and Makefiles!
March 7th, 2013, 11:19 AM
I did get this to work based on your suggestion. The funny thing about it is I tried that before and didn't work. Not sure what the issue is maybe some PFM going on.
Originally Posted by partoj
The one thing I did do was restart python...maybe that's it.
In the end thanks for your response.