Python OS Module
Introduction
In Python, the OS module gives us the means to maintain a connection between python and the operating system. OS module gives us different useful functions that can perform different tasks on the operating system level. Also, it can access any information related Operating System.
The OS modules are under python utility modules, which gives us a portable path to use OS-dependent functionality. We can also perform tasks on files and directories with this module.
To use this module, we first need to import the OS module.
import os
In the Python OS module, some methods are explained below.
os.name()
name() method gives the name of the operating system module that it imports. Right now, it registers, ‘os2’, ‘ce’, ‘nt’, ‘java’ ‘posix’, and ‘riscos’.
Code
import os
print(os.name)
Output
nt
os.mkdir()
This method is used to create a new directory.
Code
import os
os.mkdir("d:\\new")
Output
The above code will create a folder named new in the 'd' disk.
os.getcwd()
Suppose, we are working on a file, now to get the directory of that working file, we use this method.
Code
import os
print(os.getcwd())
Output
C:\BI\Python\Desktop\ModuleOS
os.chdir()
This module lets us change the current working directory to a different directory.
Code
import os
os.chdir("f:\\")
Output
f:\\
os.rmdir()
Now if we want to remove a directory from our computer we use this method to do so. First, we need to change the working directory if we are in that directory and then we can remove that folder.
Code
import os
os.rmdir("new")
Note: If we don’t change the working directory that we want to delete then we can get a permission error.
Output
It will delete the specified directory.
os.error()
This method opens up a certain file that is specified in the command. This method returns a file object connected to a pipe.
Code
import os
f = "bi.txt"
# popen() is similar to open()
file = open(f, 'w')
file.write("Welcome to Board Infinity.")
file.close()
file = open(f, 'r')
text = file.read()
print(text)
# popen() provides gateway and accesses the file directly
file = os.popen(f, 'w')
file.write("Welcome to Board Infinity.")
Output
Welcome to Board Infinity.
os.close()
This method closes the file which is linked to the file object.
Code
import os
fo = "file.txt"
f = open(fo, 'r')
t = f.read()
print(t)
os.close(f)
Output
Traceback (most recent call last):
File "main.py", line 3, in
file = open(fo, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'
Note: The same error may not be thrown, due to non-existent file or permission privilege.
os.rename()
Now while working on a file, if we want to change the name of the file, then we can use the os.rename() method to do so. If a user has the privilege then the user can rename the file.
Code
import os
fd = "board.txt"
os.rename(fd,'newboard.txt')
os.rename(fd,'newboard.txt')
Output
Traceback (most recent call last):
File "main.py", line 3, in <module>
os.rename(fd,'newboard.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'board.txt' -> 'newboard.txt'
Understanding the Output: A file name board.txt exist, thus when os.rename() is used the first time the file gets renamed. Upon the calling the function os.rename() second time, file newboard.txt exists and not board.txt thus python throws FileNotFoundError