Current drive and directory

E

EAS

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...
...
 
F

Fredrik Lundh

EAS said:
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...

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

http://docs.python.org/lib/os-file-dir.html

os.getcwd() returns the drive/path.

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

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

http://docs.python.org/lib/os-path.html

</F>
 
E

EAS

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(command)

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

EAS

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\Desktop>cd \windows\system32

C:\Documents and Settings\Erik\Desktop>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.
 
D

Diez B. Roggisch

EAS said:
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(command)

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

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.
 
E

EAS

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.)
 
J

Jeff Epler

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().
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")
first, rest = command.split(None, 1)
if first.lower() == "cd":
os.chdir(rest)
else
os.system(command)
.... but you have to do other stuff like interpretation of quoting (For
example
cd "C:\Program Files\"
), and os.chdir("c:\\Program 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
 
M

Michael Geary

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(command)
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\Desktop>cd \windows\system32

C:\Documents and Settings\Erik\Desktop>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.
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.)

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\Desktop.

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\system32 or your desktop? When you do
the "notepad calc.py" it is looking for calc.py in C:\Documents and
Settings\Erik\Desktop (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
 
G

George Kinney

EAS said:
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...

..


maybe this?
import os
os.listdir(os.path.abspath('.'))

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

???
 
D

Dennis Lee Bieber

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\Desktop>cd \windows\system32

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

--
 
H

Heather Coppersmith

maybe this?
import os
os.listdir(os.path.abspath('.'))

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
 
D

Duncan Booth

This os.system() performed a CD, and then exited -- all history
is lost.

This os.system() starts fresh, with the same settings as the
parent program.

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\system32
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\system32 && 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,047
Members
47,646
Latest member
xayaci5906

Latest Threads

Top