#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Subprocess - spaces in path


    I have the following code:

    This runs:

    s = "c:\\Users"
    check_output(s, shell=True)

    This doesn't run:

    s = "dir c:\\Users\\My Name" # space between My and Name
    check_output(s, shell=True)

    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.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    try: s="dir c:\\Users\\My"+chr(32)+"Name"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by rrashkin
    try: s="dir c:\\Users\\My"+chr(32)+"Name"
    That didn't work.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by jackmanjls
    I have the following code:

    This runs:

    s = "c:\\Users"
    check_output(s, shell=True)

    This doesn't run:

    s = "dir c:\\Users\\My Name" # space between My and Name
    check_output(s, shell=True)

    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.
    If you want to have arguments in check_output(), you need to present it as a list.

    This is not tested on windows, but this works for me:

    Code:
    # I have this file structure
    a folder/
    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
    a file
    Also, please describe what errors you get, "this runs" and "this doesn't run" are not very helpful error descriptions.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Try
    Code:
    os.listdir(r'"c:\Documents and Settings\My account\and a subdirectory"')
    Anyway, rashkin had the right idea (I'm not a regular ms dos user, so this is untested), just misquoted.

    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
    C:> prompt.

    Maybe partoj's solution fails because
    Originally Posted by wikipedia
    The dir command has existed as a DOS command since the initial release of MS-DOS. It is built into the DOS command shell, and is not provided as a separate standalone program.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by partoj
    If you want to have arguments in check_output(), you need to present it as a list.

    This is not tested on windows, but this works for me:

    Code:
    # I have this file structure
    a folder/
    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
    a file
    Also, please describe what errors you get, "this runs" and "this doesn't run" are not very helpful error descriptions.
    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.

    The one thing I did do was restart python...maybe that's it.

    In the end thanks for your response.

IMN logo majestic logo threadwatch logo seochat tools logo