By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,610 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,610 IT Pros & Developers. It's quick & easy.

Current drive and directory

P: n/a
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...
import os
print os.curdir ..print os.pardir

...
Jul 18 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"EAS" <er****@attbi.nospam.com> wrote:
Does anyone know how to display the current directory using DOS
and/or Python?


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 =---
Jul 18 '05 #2

P: n/a
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?


Jul 18 '05 #3

P: n/a
"EAS" wrote:
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>


Jul 18 '05 #4

P: n/a
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.
Jul 18 '05 #5

P: n/a
EAS wrote:
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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6

P: n/a
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.)
Jul 18 '05 #7

P: n/a
> 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
Jul 18 '05 #8

P: n/a
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

Jul 18 '05 #9

P: n/a

"EAS" <er****@attbi.nospam.com> wrote in message
news:LP7tc.64476$gr.6380832@attbi_s52...
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...
import os
print os.curdir .print os.pardir

..

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

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

???
Jul 18 '05 #10

P: n/a
On Wed, 26 May 2004 22:02:06 GMT, "EAS" <er****@attbi.nospam.com>
declaimed the following in comp.lang.python:
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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #11

P: n/a
On Wed, 26 May 2004 20:33:43 -0400,
"George Kinney" <gk*****@yahoo.com> wrote:
"EAS" <er****@attbi.nospam.com> wrote in message
news:LP7tc.64476$gr.6380832@attbi_s52...
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...
>>>import os
>>>print os.curdir .
>>>print os.pardir

..


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

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli
Jul 18 '05 #12

P: n/a
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote in
news:56********************************@4ax.com:
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.


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.
Jul 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.