Current drive and directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • EAS

    Current drive and directory

    Does anyone know how to display the current directory using DOS
    and/or Python? I already tried os.pardir and os.curdir in Python, but all
    they return are
    a couple of periods...
    [color=blue][color=green][color=darkred]
    >>>import os
    >>>print os.curdir[/color][/color][/color]
    ..[color=blue][color=green][color=darkred]
    >>>print os.pardir[/color][/color][/color]
    ...


  • beliavsky@aol.com

    #2
    Re: Current drive and directory


    "EAS" <eriksp@attbi.n ospam.com> wrote:[color=blue]
    >Does anyone know how to display the current directory using DOS
    >and/or Python?[/color]

    Use the getcwd() function in the os module.



    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

    Comment

    • EAS

      #3
      Re: Current drive and directory

      Howcome when I run this script:

      print """MS-DOS Prompt
      'Q' to Quit"""

      import os
      command = ""

      while command.lower() != "q":
      directory = os.getcwd()
      print "\n", directory, "\b>",
      command = raw_input("\b")
      os.system(comma nd)

      it doesn't update the directory when I use 'cd' for the command prompt?




      Comment

      • Fredrik Lundh

        #4
        Re: Current drive and directory

        "EAS" wrote:[color=blue]
        > Does anyone know how to display the current directory using DOS
        > and/or Python? I already tried os.pardir and os.curdir in Python, but all
        > they return are a couple of periods...[/color]

        did you read the fine documentation? look under "files and
        directories":



        os.getcwd() returns the drive/path.

        you can use os.path.splitdr ive(os.getcwd() ) to get the drive
        and the path as two separate strings.

        curdir etc is documented a little later in the same chapter:



        </F>




        Comment

        • EAS

          #5
          Re: Current drive and directory

          And also, when I actually run the program, I have trouble using notepad on
          files:

          MS-DOS Prompt
          'Q' to Quit

          C:\Documents and Settings\Erik\D esktop>cd \windows\system 32

          C:\Documents and Settings\Erik\D esktop>notepad calc.py

          It opens up notepad, but syas that it can't find the file. I know it's in
          there,
          and I know that's the exact name.


          Comment

          • Diez B. Roggisch

            #6
            Re: Current drive and directory

            EAS wrote:
            [color=blue]
            > Howcome when I run this script:
            >
            > print """MS-DOS Prompt
            > 'Q' to Quit"""
            >
            > import os
            > command = ""
            >
            > while command.lower() != "q":
            > directory = os.getcwd()
            > print "\n", directory, "\b>",
            > command = raw_input("\b")
            > os.system(comma nd)
            >
            > it doesn't update the directory when I use 'cd' for the command prompt?[/color]

            I'm no windows user, but under unix a child process (which is created using
            os.system) inherits the environment of its porent process as a copy - so
            chtanges in the childs env don't affecth the parents one. Which is
            probablpy a good idea, as ontherwise I'd be able to manipulate the calling
            process in undesirable ways.

            So I think you need some parsing to intercept a cd and change your current
            working dir using the os module.

            Aren't there more shell like interactive pythons out there - ipython or so?
            Look at them, they might be what you seem to be after here.

            --
            Regards,

            Diez B. Roggisch

            Comment

            • EAS

              #7
              Re: Current drive and directory

              Ok, I guess that won't work without some complicated scripting, but is there
              a command in
              DOS that shows the current directory? (so i could just type it in when using
              the prompt.)


              Comment

              • Michael Geary

                #8
                Re: Current drive and directory

                > print """MS-DOS Prompt[color=blue]
                > 'Q' to Quit"""
                >
                > import os
                > command = ""
                >
                > while command.lower() != "q":
                > directory = os.getcwd()
                > print "\n", directory, "\b>",
                > command = raw_input("\b")
                > os.system(comma nd)[/color]
                [color=blue]
                > And also, when I actually run the program, I have trouble using
                > notepad on files:
                >
                > MS-DOS Prompt
                > 'Q' to Quit
                >
                > C:\Documents and Settings\Erik\D esktop>cd \windows\system 32
                >
                > C:\Documents and Settings\Erik\D esktop>notepad calc.py
                >
                > It opens up notepad, but syas that it can't find the file. I know it's in
                > there, and I know that's the exact name.[/color]
                [color=blue]
                > Ok, I guess that won't work without some complicated scripting, but
                > is there a command in DOS that shows the current directory? (so i
                > could just type it in when using the prompt.)[/color]

                The CD command with no arguments does that. Same as pwd in Unix. In this
                case, it should print the same thing as your prompt: C:\Documents and
                Settings\Erik\D esktop.

                BTW, you're not running MS-DOS at all. It appears that you are on Windows
                2000 or XP, so when you call os.system() you are calling cmd.exe, which is a
                32-bit console application, not a DOS application. And you're starting a
                new, temporary instance of cmd.exe for each command, which is why your CD
                command isn't having any effect.

                What directory is calc.py in? \windows\system 32 or your desktop? When you do
                the "notepad calc.py" it is looking for calc.py in C:\Documents and
                Settings\Erik\D esktop (your desktop), because that is your current
                directory. If you do a File/Save... in Notepad you can confirm what Notepad
                is using for the current directory.

                What is it you are trying to do here? Maybe there is another way to approach
                it that will work better.

                -Mike


                Comment

                • Jeff Epler

                  #9
                  Re: Current drive and directory

                  You'll have to parse the command yourself. If it's an "internal
                  command" (as cd must be), then execute it in Python, not via os.system().
                  [color=blue]
                  > print """MS-DOS Prompt
                  > 'Q' to Quit"""
                  >
                  > import os
                  > command = ""
                  >
                  > while command.lower() != "q":
                  > directory = os.getcwd()
                  > print "\n", directory, "\b>",
                  > command = raw_input("\b")[/color]
                  first, rest = command.split(N one, 1)
                  if first.lower() == "cd":
                  os.chdir(rest)
                  else[color=blue]
                  > os.system(comma nd)[/color]
                  .... but you have to do other stuff like interpretation of quoting (For
                  example
                  cd "C:\Program Files\"
                  ), and os.chdir("c:\\P rogram Files\\") behaves differently than typing
                  "cd "C:\Program Files\" in command.com/cmd.exe (because the latter doesn't
                  change the current drive letter, it just changes the current directory
                  associated with the given drive letter)

                  Other things that must be done in Python, not by system():
                  * "set" for environment variables
                  * "a:" "b:" etc to change
                  * @echo off
                  * flow control (IF ERRORLEVEL, GOTO, etc)
                  Some things are traditionally implemented in the shell, but may
                  not work if passed to system():
                  * redirection
                  * pipelines
                  * any advanced feature in a unix shell, like job control
                  If you want to do a shell right, there's a lot of work involved.

                  Jeff

                  Comment

                  • George Kinney

                    #10
                    Re: Current drive and directory


                    "EAS" <eriksp@attbi.n ospam.com> wrote in message
                    news:LP7tc.6447 6$gr.6380832@at tbi_s52...[color=blue]
                    > Does anyone know how to display the current directory using DOS
                    > and/or Python? I already tried os.pardir and os.curdir in Python, but all
                    > they return are
                    > a couple of periods...
                    >[color=green][color=darkred]
                    > >>>import os
                    > >>>print os.curdir[/color][/color]
                    > .[color=green][color=darkred]
                    > >>>print os.pardir[/color][/color]
                    > ..[/color]


                    maybe this?
                    import os
                    os.listdir(os.p ath.abspath('.' ))

                    os maybe:
                    os.walk(os.path .abspath('.'))

                    ???


                    Comment

                    • Dennis Lee Bieber

                      #11
                      Re: Current drive and directory

                      On Wed, 26 May 2004 22:02:06 GMT, "EAS" <eriksp@attbi.n ospam.com>
                      declaimed the following in comp.lang.pytho n:
                      [color=blue]
                      > And also, when I actually run the program, I have trouble using notepad on
                      > files:
                      >
                      > MS-DOS Prompt
                      > 'Q' to Quit
                      >
                      > C:\Documents and Settings\Erik\D esktop>cd \windows\system 32[/color]

                      This os.system() performed a CD, and then exited -- all history
                      is lost.
                      [color=blue]
                      >
                      > C:\Documents and Settings\Erik\D esktop>notepad calc.py
                      >[/color]
                      This os.system() starts fresh, with the same settings as the
                      parent program.

                      --[color=blue]
                      > =============== =============== =============== =============== == <
                      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > wulfraed@dm.net | Bestiaria Support Staff <
                      > =============== =============== =============== =============== == <
                      > Home Page: <http://www.dm.net/~wulfraed/> <
                      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                      Comment

                      • Heather Coppersmith

                        #12
                        Re: Current drive and directory

                        On Wed, 26 May 2004 20:33:43 -0400,
                        "George Kinney" <gk_2345@yahoo. com> wrote:
                        [color=blue]
                        > "EAS" <eriksp@attbi.n ospam.com> wrote in message
                        > news:LP7tc.6447 6$gr.6380832@at tbi_s52...[color=green]
                        >> Does anyone know how to display the current directory using DOS
                        >> and/or Python? I already tried os.pardir and os.curdir in Python, but all
                        >> they return are
                        >> a couple of periods...
                        >>[color=darkred]
                        >> >>>import os
                        >> >>>print os.curdir[/color]
                        >> .[color=darkred]
                        >> >>>print os.pardir[/color]
                        >> ..[/color][/color]

                        [color=blue]
                        > maybe this?
                        > import os
                        > os.listdir(os.p ath.abspath('.' ))[/color]

                        Or, using the information we gained by peeking at os.curdir:

                        os.listdir( os.path.abspath ( os.curdir ) )

                        I know the OP said DOS, but that may change some day.

                        Regards,
                        Heather

                        --
                        Heather Coppersmith
                        That's not right; that's not even wrong. -- Wolfgang Pauli

                        Comment

                        • Duncan Booth

                          #13
                          Re: Current drive and directory

                          Dennis Lee Bieber <wlfraed@ix.net com.com> wrote in
                          news:56oab0lrlg 9dit9g19qdrargp ominsqkj0@4ax.c om:
                          [color=blue][color=green]
                          >> And also, when I actually run the program, I have trouble using
                          >> notepad on files:
                          >>
                          >> MS-DOS Prompt
                          >> 'Q' to Quit
                          >>
                          >> C:\Documents and Settings\Erik\D esktop>cd \windows\system 32[/color]
                          >
                          > This os.system() performed a CD, and then exited -- all history
                          > is lost.
                          >[color=green]
                          >>
                          >> C:\Documents and Settings\Erik\D esktop>notepad calc.py
                          >>[/color]
                          > This os.system() starts fresh, with the same settings as the
                          > parent program.
                          >[/color]

                          Yes, that is what os.system does. It runs a single command, so it is
                          roughly equivalent in this case to entering:

                          cmd /c cd \windows\system 32
                          cmd /c notepad calc.py

                          at a windows command prompt. i.e. A separate shell is started, and
                          terminated for each command.

                          To get what you want, you must execute the commands in the same shell. Some
                          ways to do this:

                          For a short sequence of commands, just use the command separator as you
                          would normally when entering multiple commands on one line at a command
                          prompt. e.g.

                          os.system(r"cd \windows\system 32 && notepad calc.py")

                          For a longer sequence, you can write a temporary batch file then use
                          os.system to execute it.

                          If you need more complex interaction you might want to use the popen2
                          module to pipe commands in and read the output, but you will almost
                          certainly need to use multiple threads if you try this otherwise your
                          program will deadlock.

                          Or, for this specific case, just use absolute pathnames for everything so
                          you don't have to change current directory before running a command.

                          Comment

                          Working...