Re: How to run commands in command line from a script

Discussion in 'Python' started by Ivan Shevanski, Jul 1, 2005.

  1. Alright well I'm quite a noob and when I run a simple command to change the
    current directory, nothing happens. I made a little test script to show it:


    import os
    cwd = os.getcwd()
    print cwd
    os.system('cd = C:\Program Files')
    print cwd


    then the result:

    C:\Python24\Python Scripts
    C:\Python24\Python Scripts

    The cd never changed. . . Can somebody explain this? I think I need to add
    something simple, i didnt quite get the example ' os.system('./some
    --command') '


    -Ivan




    >From: Robert Kern <>
    >To:
    >Subject: Re: How to run commands in command line from a script
    >Date: Thu, 30 Jun 2005 20:01:29 -0700
    >
    >Ivan Shevanski wrote:
    > > I know there is an easy way to do this, and I just can not remember. I

    >have
    > > already searched where I thought the module would be. . . I just want to

    >run
    > > some name specific commands. Is this easily possible?

    >
    >Quick and dirty:
    >
    > import os
    > os.system('./some --command')
    >
    >More robust: Use the subprocess module.
    >
    >--
    >Robert Kern
    >
    >
    >"In the fields of hell where the grass grows high
    > Are the graves of dreams allowed to die."
    > -- Richard Harter
    >

    --

    _________________________________________________________________
    Don’t just search. Find. Check out the new MSN Search!
    http://search.msn.click-url.com/go/onm00200636ave/direct/01/
    Ivan Shevanski, Jul 1, 2005
    #1
    1. Advertising

  2. Ivan Shevanski

    Peter Hansen Guest

    Ivan Shevanski wrote:
    > Alright well I'm quite a noob and when I run a simple command to change
    > the current directory, nothing happens. I made a little test script to
    > show it:
    >
    >
    > import os
    > cwd = os.getcwd()
    > print cwd
    > os.system('cd = C:\Program Files')
    > print cwd


    There are at least three errors in the above.

    1. The command is "cd <path>", not "cd = <path>". (Wow! Just tried
    it... okay, technically it appears to work, but it doesn't seem to be
    the documented approach, and I've _never_ seen it before, but I have to
    admit it works. :)

    Okay then, two errors:

    1. You are using a backslash without properly escaping it (as \\) or
    using a "raw" string (prefix the string with r as in r"my\string".
    Well, okay, in _this_ particular case you get away with it, because the
    \P sequence is not a special one, but if you'd use \t or \b or something
    like that it would not have worked.

    So, then, only one error:

    1. Commands like "cd" are executed by the shell, not as separate
    executables (.exe files). Unfortunately, the result of this is that
    their effects are lost when the shell returns to Python, so you can't do
    what you are trying to do in quite this manner.

    Generally, the only way to use an application (i.e. a program like the
    Python interpreter, or your own .exe) to change the working folder is to
    have your script executed from within a batch file, write out a new
    batch file in your script, and then have the calling batch file execute
    that script if it exists. Roughly like this:

    --------file mycd.bat----------
    @echo off
    if exist c:\temp\_mycd.bat del c:\temp\_mycd.bat
    c:\python24\python.exe myscript.py
    rem Note: file _mycd.bat written by myscript.py
    if exist c:\temp\_mycd.bat call c:\temp\_mycd.bat

    --------somewhere inside myscript.py--------
    newDir = r'c:\Program Files'
    f = open(r'c:\temp\_mycd.bat', 'w')
    f.write('''rem This file generated by myscript.py
    cd = %s''' % newDir)
    f.close()

    --------file _mycd.bat-----------
    rem This file generated by myscript.py
    cd = c:\Program Files


    Hope that helps... :)

    -Peter
    Peter Hansen, Jul 1, 2005
    #2
    1. Advertising

  3. Ivan Shevanski

    Peter Hansen Guest

    Peter Hansen wrote:
    > Ivan Shevanski wrote:
    >> Alright well I'm quite a noob and when I run a simple command to
    >> change the current directory, nothing happens. I made a little test
    >> script to show it:

    >
    > Generally, the only way to use an application (i.e. a program like the
    > Python interpreter, or your own .exe) to change the working folder is to
    > have your script executed from within a batch file, write out a new
    > batch file in your script, and then have the calling batch file execute
    > that script if it exists.


    And, after that long-winded reply, here's another option, if you're just
    trying to change the directory _within_ your app but don't care if the
    change persists:

    import os
    os.chdir(r'c:\Program Files')

    -Peter
    Peter Hansen, Jul 1, 2005
    #3
  4. Ivan Shevanski

    Mike Meyer Guest

    Peter Hansen <> writes:

    > Ivan Shevanski wrote:
    >> Alright well I'm quite a noob and when I run a simple command to
    >> change the current directory, nothing happens. I made a little test
    >> script to show it:
    >> import os
    >> cwd = os.getcwd()
    >> print cwd
    >> os.system('cd = C:\Program Files')
    >> print cwd

    >
    > 1. Commands like "cd" are executed by the shell, not as separate
    > executables (.exe files). Unfortunately, the result of this is that
    > their effects are lost when the shell returns to Python, so you can't
    > do what you are trying to do in quite this manner.


    It doesn't really matter that it's executed by the shell or as a
    seperate exe. Things run via os.system don't happen in the Python
    process, so any pre-process changes that they make won't show up in
    the Python process.

    Of course, shells suffer from the same problem. If they run another
    process to make a per-process change, the change won't happen in the
    shell process. So all the commands that make per-process changes -
    like cd - are handled internally by the shell.

    The trick is figuring out what things are per-process changes, and
    what are global state changes, and then using the Python tools for
    making the per-process changes. Since using the Python tools for
    making global changes is usually more portable than invoking an
    external command, you should try and use such tools for those changes
    as well.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jul 1, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Cowald
    Replies:
    4
    Views:
    1,018
    Tohid
    Oct 23, 2003
  2. Ivan Shevanski
    Replies:
    1
    Views:
    378
    Peter Hansen
    Jul 1, 2005
  3. Replies:
    1
    Views:
    317
    Larry Bates
    Dec 14, 2006
  4. Sonu
    Replies:
    2
    Views:
    354
    Alex Popescu
    Aug 5, 2007
  5. Replies:
    12
    Views:
    180
    Tintin
    Jul 30, 2005
Loading...

Share This Page