Thursday, February 8, 2018

OS module in python

The os module in python interacts directly with the operating system.The os module is a part of the stdlib in python but we need to import it.But no need to download it.

The os module  is generally used to list the directories ,create folder , remove folder, check the working directory and to change the path of the working directory.

How to import os module in python ?
import os

To get the current working directory :
currdir = os.getcwd()
print(currdir)
#output :- C:\Users\sangam\Documents

To make a new directory:
 os.mkdir("os_module_test")
 The required directory will get created in the default path.

To rename the directory:
os.rename("os_module_test","os_module_test2")
The above command will rename the directory os_module_test into os_module_test2.

To remove a file:
os.remove("train.csv")

To get the current process id :
os.getpid()


These are the command that are commonly used in the python but there are variety of operation that can be done by os module.

Further Reading :- https://docs.python.org/3.1/library/os.html



No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...