The Python OS library was created to be able to interact with the Operating system of a computer and carry out OS related tasks. The OS library can carry out tasks like creating and removing directories, change the current working directory, open and close files.
Importing OS library
OS library is part of the Python Standard library, so no need to download and install it.
import os
Directory related Functions
os.getcwd
The os.getcwd()
returns the current working directory of the program. By default, the current working directory is the file path of the python program. If a file/folder is the current working directory, there is no need to write down the full file path to access it; only the name is required.
print(os.getcwd())
os.chdir
The os.chdir
changes the current working directory of the program. The example below changes the current working directory to D drive.
print(os.getcwd())
os.chdir("D://"))
print(os.getcwd())
OUTPUT:
C:\Users\Default\Desktop
D:\
os.mkdir
The os.mkdir
function creates an empty directory of a specified file path. The end result in this example will basically be a new folder at the file path “C:/Users/Default”.
os.mkdir("C://Users/Default/Newfolder")
os.listdir
The os.listdir
function lists the contents of each individual file and folder in a given directory. The following code will proceed to print out the paths of every single file, folder and subfolder individually.
os.listdir("C://Users")
os.walk
The os.walk
walks through all the folders and subfolders in a directory. This function comes in handy when searching through your computer for files. Run the code below to truly understand how this function works.
for folder, subfolders, files in os.walk("C:\\Users"):
print(folder)
for subfolders in subfolders:
print(subfolders)
for file in files:
print(file)
The os.walk
function returns three values. A folder’s file path, the names of the sub-folders in it, and the names of the files within the sub-folders. If you’re creative you can play around with this function to achieve many things.
File Handling Functions
os.startfile
Another handy function is startfile()
. This function takes two parameters, a file path and an operation. The file path is compulsory, whereas we can leave the operation to it’s default value.
When passing in a single parameter, the startfile()
opens the file at the file path you gave, just like you would regularly open/run it (by double clicking). The below code will open up the “kitten.jpg” image in the default image viewer installed on any computer.
import os
os.startfile("kitten.jpg")
If you give it the path to an exe
, the exe
will be executed. If you give it the path of a .py
file, it will also be executed. startfile()
is a simple but powerful function.
Examples of other operations include “print
” and “edit
” (for files) and “explore
” and “find
” (for directories/folders). (These are placed in the second parameter).
os.popen
OS library has the ability to open files to read and write data to them. The default mode is the read state, denoted by ‘r’. The other state is the write file, denoted by ‘w’. The os.popen()
takes 2 parameters. The first is the name of the file to be read/written to, and the second is the mode.
File = popen("NewFile.txt", 'w')
File.write("Hello World")
File.close
File = popen("NewFile.txt", 'r')
print(File.read())
File.close
os.close
Once you have opened a file for read and write purposes, you have to close it using the os.close()
function. Think of it as closing the door when leaving a room.
os.rename
The os.rename
function takes two parameters. The first is the file path of the file whose name is to be changed. The second is the new name to which you want the old file name to change to.
os.rename("Old.txt","New.txt")
Misc. Functions
The os.getlogin()
functions returns the name of the user logged in on the controlling terminal of the process
user_name = os.getlogin()
print(user_name)
CodersLegacy
A link to the back main section for Python Libraries: link
There is a lot more to the os
library than what we have covered. If you’re interested in the inner workings of the operating systems, (processes and all) head over to the documentation of the os
library here!