Explanation of the OS library in Python - ProgrammerTech


Explanation of the OS library in Python

  • Share this:
Explanation of the OS library in Python

os library in python

The OS library allows us to perform some commands related to our operating system, (create folders, delete, rename .... etc., although we can perform most of these commands manually, but in most cases it is necessary to automate these tasks When writing programs in Python.

Many of the tasks that we perform at the operating system level can be done through the use of the os library, and these tasks include:

  1. Find out some information about the operating system.
  2. Perform operations on folders (create, copy, delete).
  3. Dealing with files in the operating system and modifying or creating them.
  4. Deal with system variables and know information about them.

In today's lesson, we will talk about os module, and from the name it is clear that it deals with operating systems. All method is easy to handle.

Calling the os library in Python

>>>import os


Rename files and folders os.rename

In order to rename an existing file with the name programmer.txt to programmertech.txt in the current directory, we use the os.rename function in this command, that is, if we want to change the name of a file, we go to its path first by shelling Python, then put a comma and put the new path and name As the following example:

Example 1

>>>os.rename ("C:\\Users\\sama\\Desktop\\programmer.txt", "C:\\Users\\sama\\Desktop\\programmertech.txt")

First we used to change the name os.rename then specify the path of the file we want to rename on the desktop as programmer.txt and put a comma and then put the path again with the file name changed to the new name programmertech.txt and when we hit the Enter button directly the name was changed The file has its new name on the desktop.


delete os.remove files

In order to delete files and folders we use os.remove, the os.remove function we can use to delete files, and if the file does not exist, an error will appear in the current path and an error will also appear if the file is being used by another program Or you do not have the necessary permissions to delete this file.

Example 2

>>>os.remove ("C:\\Users\\sama\\Desktop\\programmertech.txt")

When you press Enter, the file will disappear from the desktop and be permanently deleted.


Create os.mkdir folders

By using os.mkdir, we can create a folder or multiple folders using the os.mkdir and os.makedirs functions. The difference between the two functions is that the first function creates only one folder in the current working path, while the second function creates Several folders in one pass.

Example 3

>>>os.mkdir ("C:\\Users\\sama\\Desktop\\Muhammad")
>>>os.makedirs ("C:\\Users\\sama\\Desktop\\Muhammad1\Muhammad2\Muhammad3\Muhammad4")

In the first line we used os.mkdir to create a single folder named Muhammad, while in the second line we used the os.makedirs function to create several folders inside a parent folder, which is Muhammad, and inside it will be the folder Muhammad1 and within it Muhammad2, then Muhammad3, then Muhammad4.


Delete folders os.rmdir

In order to delete a folder we can use the os.rmdir function with the same restrictions. If the folder does not exist, an error will appear when calling the function and you must also have permission to delete this folder, and this must be empty before the deletion process.

>>>os.rmdir ("C:\\Users\\sama\\Desktop\\Muhammad")

This will delete the Muhammad folder that we created.


Find out the current path os.getcwd

The os library in Python provides functions to deal with the current working directory, and this gives us a strong advantage when building programs and applications in Python, and to identify the current working path we use the os.getcwd function, by using the os.getcwd command it can give us the path The current we are working with.


It will give us the path they are currently in that we are working on in Python.


Change the workflow os.chdir

The os.chdir function helps us change the current working path of the Python program in the session we are working on.

>>>os.chdir ("C:\\Users\\sama\\Desktop\\")

In this example we changed the current work path on the desktop.


Find out which paths os.listdir is in

Using os.listdir() we can see everything in the current path, ie if we are on the desktop it will show us everything on the desktop.



Find out which files have executable files

The os.get_exec_path command gives us the paths that contain executable files like the python interpreter and so on.



Find out the current username os.getlogin

With the os.getlogin command, we can find out the current user name of the operating system if there are multiple users of the system.



Open a command prompt and type os.popen

The os.popen command enables us to open a command prompt in the system and type in it.


We told him to open a CMD command prompt and create a command for us, execute it on the system, and read it.


Dealing with path in Python

The path submodule in the OS library plays a major role in facilitating the handling of files at the operating system level in Python, and the path module offers many great capabilities.

The function os.path.basename returns the file name from a specific path, which means if we assume that we want to get the file name from a specific path, we will pass the path to the function.

The command to invoke the path submodule in the os . library

>>>import os.path


Ensure that there is a specific path isdir


We told him whether C is a path present on this computer or not. He will reply to us with True because it is an existing path.


Ensure that there is an isfile


We specified the path of an image named data that is on the desktop. We told him whether the image is on the desktop or not, and he will reply with true or false.


Merge two tracks together


In this case, he will merge the C path with the user sama.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com