Showing posts with label running os commands from python idle. Show all posts
Showing posts with label running os commands from python idle. Show all posts

Thursday, August 27, 2015

How To Run OS Commands From Pytohn IDLE

Python has a inbuilt module by which we can run windows or Linux commands which we usually enter in the command prompt or linux terminal. This is very cool. First of all open your idle and import the os module.
>> > import os

Please see help(os) to find more about the module, as it is always a good practice to learn about the module before using it.
You will find a system function in the os module which we will be using to access system commands.

If we want to make a directory then for windows the command is  -

>>>command="mkdir myname"
>>>os.system(command)
Access id denied (Oops did not expect it)
So, where you are in the OS you are making the directory. To check out about the current directory write -
>>> os.getcwd()
'c:\\windows\\system32'
This is a system folder , hence the accessing problem To change the current working directory do the following -
>>> os.chdir('C:\Users\someuser\Documents\python')
This will change the current working directory. Now you can give the previous command for creating directory. You can do nearly whatever a windows command prompt can do with the os.system(). Lets see another useful command -

>>> command = "ping 127.0.0.1"
>>>os.system(command)

This will ping to the loopback address. Cool ! keep experimenting , do the telnetting as previously stated.
And don't forget to share also please visit my website www.pythonbeginner.in for more.

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...