Thread: System Call

    #1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Red face System Call


    I have this little code that uses a system call to bring up an image viewer to show a picture. Looks like I get a problem with spaces in the directory name.
    Code:
    import os
    fname = "Ethel.gif"
    os.system("C:/Program Files/ImView/ImView.exe %s" % fname)
    I copied ImView.exe to another directory without spaces and it works fine. That space thing bothers me, since Mr. Bill put that in there! Are there any secrets to this?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Try:
    Code:
    import os
    fname = 'Ethel.gif'
    os.system('C:\\Program Files\\ImView\\ImView.exe ' + fname)
    Your slashes are not backslashes when they should be. You are using regular slashes, not good.

    PS. Lol, Mr. Bill. It took me a few seconds to figure out who you were referring to.

    Comments on this post

    • monkeyman23555 agrees : ya me too lol
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Red face


    Originally Posted by †Yegg†
    Try:
    Code:
    import os
    fname = 'Ethel.gif'
    os.system('C:\\Program Files\\ImView\\ImView.exe ' + fname)
    Your slashes are not backslashes when they should be. You are using regular slashes, not good.

    PS. Lol, Mr. Bill. It took me a few seconds to figure out who you were referring to.
    Sorry, backslashes won't do it, still gives me the same error:
    'C:\Program' is not recognized ...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Atlantic City, NJ
    Posts
    327
    Rep Power
    13
    Make it a raw string:

    Code:
    import os
    fname = 'Ethel.gif'
    os.system(r'C:\Program Files\ImView\ImView.exe ' + fname)
    I'll learn this stuff someday.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You should be able to fix the problem by escaping the space with a backslash. I would also suggest that you stick with the forwardslash in your paths since it looks cleaner, doesn't need to be escaped. Rest assured that this wont cause you any problems .

    You should also be able to use the alias c:\progra~1 which should sercumvent the space problem however I haven't been able to test either of these solusions since I don't have access to Python on Windows right now.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14

    Exclamation


    Spaces in paths cause lots of problems so Microsoft kludged the kludge to work around them. Putting double quotes around the path tells the OS to use the whole string including spaces as a single command.

    os.system(' "C:\\Program Files\\ImView\\ImView.exe" ' + fname)

    should work. It can get messy if the fname also contains spaces.

    grim
  12. #7
  13. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65

    Smile


    Great suggestions! Tested them all, but only this one works:
    Code:
    import os
    fname = 'Ethel.gif'
    os.system('"C:\Program Files\ImView\ImView.exe" ' + fname)
    Notice the space between " and '

IMN logo majestic logo threadwatch logo seochat tools logo