473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Current drive and directory

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
12 15768

"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
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
"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
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
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
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
> 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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jenny | last post by:
Hi, I am trying to find a VB way that would create a folder on all existing drives - the folder name would be the same on each drive. ie c:\backup, d:\backup, etc. But the folders would only be...
2
by: The Roys | last post by:
Hi I have a need to be able to simulate a d/click on a Dir box item. When a certain event occurs I want the program to open up the directory above the current one selected and refresh. I can...
5
by: worzel | last post by:
How do I have my win forms app find my configuration file. I want to have this conf file stored in the same folder as the exectuable and simply refer to it using a relative path. All seemed good...
1
by: Max Baki via .NET 247 | last post by:
I all, i've write a sample ftp Windows service which download files and copy them on remote share or remote mapped drive (the service works fine on local drives). This part of code is the...
1
by: Simon | last post by:
Dear Access friends, How can I load a string with his own folder address. The following code addressed to the system folder of MS programs.
0
by: dgk | last post by:
Is there a FAQ somewhere about how to publish a web app to a (2003) server? I've mapped a drive to a directory under inetpub/wwwroot, and used the Visual Studio "Publish Web Site" menu option to...
14
by: gio | last post by:
I have a problem and the solution should works under windows and unix OS. Suppose I have a program ex.c in the directory X (so the current working directory of ex.c is X). Also suppose I have...
23
by: Rotsey | last post by:
Hi, I am writing an app that scans hard drives and logs info about every fine on the drive. The first iteration of my code used a class and a generic list to store the data and rhis took...
1
by: romcab | last post by:
Hi guys, I created an application in vb6 which reads a textfile which is currently on a default drive which is c:. What I want to do is to get the current directory where my application is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.