473,657 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15820

"EAS" <er****@attbi.n ospam.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(comma nd)

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

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\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.
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(comma nd)

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(comma nd) 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. 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\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
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(N one, 1)
if first.lower() == "cd":
os.chdir(rest)
else os.system(comma nd)

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

Jul 18 '05 #9

"EAS" <er****@attbi.n ospam.com> wrote in message
news:LP7tc.6447 6$gr.6380832@at tbi_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.p ath.abspath('.' ))

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

???
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7192
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 created if they don't already exist, and if the drive happens to be one a folder cannot be created on (ie a cdrom drive) it would just be skipped without the code generating any errors. your help on this would be most appreciated. jenny
2
2413
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 obtain the directory by "Dir1.List(Dir1.ListIndex - 1)", but how do I get the program to open it and refresh the Dir box. Thanks Trevor.
5
332
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 untill I opened some files using the "browse" dialog box - this seems to have changed my apps notion of what the "relative" position of files is. For now I have hard coded my conf file in c: drive. Of course, I can't depend on this for the app...
1
3411
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 function to change the local path for downloaded files. If the configured path is different from local drive C: the new path is selected. The program works fine in debug or release mode into Visual Studio. All downloaded files were placed on remote...
1
3610
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
3509
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 build and publish it. The app files show up on the server. I went into IIS and created it the app as a virtual directory. Now IIS looks good, with the gear icon on the directory, but trying to access the application via IE gives this message: ...
14
13532
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 this code fragment: .... char otherpath; char cmd;
23
3731
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 13min on my 60 GB drive. I wanted it to be quicker.
1
8968
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 installed eliminated the default drive. How can I do that in vb6?
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.