•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 428,371 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,506 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 446 | Replies: 5
![]() |
indeed it is possible, very easy actually
and if you want to make it so that you can type commands into your python commandline
One thing to note is that if you are just going to make specific calls to the command line to do one thing such as cd directory, you should use the python os module rather than just calling the comman shell.
To perform a cd command from python you ould use the following
This changes the current working directory to C:\Folder. From there you cann call other functions to make directories etc.
Python: os module or type
you should also look at os.name and os.path especially,
Hope this helps, just be careful if you do allow user input to access the command prompt.
Chris
Python Syntax (Toggle Plain Text)
import os os.system("cd [directory]")
and if you want to make it so that you can type commands into your python commandline
Python Syntax (Toggle Plain Text)
import os while True: command = raw_input("Enter Command->") if command.upper() == "Q": break try: os.system(command) except: print "invalid command\n"
One thing to note is that if you are just going to make specific calls to the command line to do one thing such as cd directory, you should use the python os module rather than just calling the comman shell.
To perform a cd command from python you ould use the following
Python Syntax (Toggle Plain Text)
import os os.chdir("C:\\A folder")
This changes the current working directory to C:\Folder. From there you cann call other functions to make directories etc.
Python: os module or type
help("os") in the Python command lineyou should also look at os.name and os.path especially,
help("os.name") etc for information on these.Hope this helps, just be careful if you do allow user input to access the command prompt.
Chris
Last edited by Freaky_Chris : May 21st, 2008 at 4:20 am.
You should look into the os module as i suggested as this allows you to do alot of things. here is an example
ofcourse this is a very basic example and can be expanded greatly. You can loop through files
so as you can see this way you can do alot of things this way. You can store the files use them to do other things with whatever.
Chris
Python Syntax (Toggle Plain Text)
import os for path, dirs, files in os.walk("C:"): print path, dir, files
ofcourse this is a very basic example and can be expanded greatly. You can loop through files
Python Syntax (Toggle Plain Text)
for path, dirs, files in os.walk("C:"): for f in files: print f
so as you can see this way you can do alot of things this way. You can store the files use them to do other things with whatever.
Chris
•
•
Join Date: Mar 2008
Posts: 15
Reputation:
Rep Power: 1
Solved Threads: 1
You should indeed use existing/available python modules as you have many built-in functions available for exactly what you need instead of having to sift through output to do something specific. Using python modules instead of calling os-specific commands keeps your programs portable and they perform better.
However, just in case you wish to capture output from external commands, you can use popen:
There is module called "commands" which provides a nice wrapper around popen but I have not tried it on Windows.
HTH
However, just in case you wish to capture output from external commands, you can use popen:
python Syntax (Toggle Plain Text)
import os p = os.popen("dir") for i in p: print i.strip() # prints the dir output one line at a time p.close()
There is module called "commands" which provides a nice wrapper around popen but I have not tried it on Windows.
HTH
Last edited by rikxik : May 23rd, 2008 at 3:58 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: TCP SocketServer problem ( and hi, im new )
- Next Thread: Can someone help me improve this function?


Linear Mode