Skip to content

OS Module


It is possible to automatically perform many operating system tasks. The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc.

You first need to import the os module to interact with the underlying operating system. So, import it using the import os statement before using its functions. os``import os

The getcwd() function confirms returns the current working directory. getcwd()

import os
print(os.getcwd()) #output: 'C:\Python37'

`import os

print(os.getcwd()) #output: ‘C:\Python37’`

We can create a new directory using the os.mkdir() function, as shown below. os.mkdir()

import os
os.mkdir("C:MyPythonProject")

`import os

os.mkdir(“C:MyPythonProject”)A new directory corresponding to the path in the string argument of the function will be created. If you open the C:\ drive, then you will see the MyPythonProject folder has been created.C:`MyPythonProject By default, if you don’t specify the whole path in the mkdir() function, it will create the specified directory in the current working directory or drive. The following will create MyPythonProject in the C:\Python37 directory. mkdir()``MyPythonProject``C:\Python37

import os
print(os.getcwd()) #output: 'C:Python37'
os.mkdir("MyPythonProject")

`import os

print(os.getcwd()) #output: ‘C:Python37’ os.mkdir(“MyPythonProject”)`

We must first change the current working directory to a newly created one before doing any operations in it. This is done using the chdir() function. The following change current working directory to C:\MyPythonProject. chdir()``C:\MyPythonProject

import os
os.chdir("C:MyPythonProject") # changing current workign directory
print(os.getcwd()) #output: 'C:MyPythonProject'

`import os

os.chdir(“C:MyPythonProject”) # changing current workign directory print(os.getcwd()) #output: ‘C:MyPythonProject’You can change the current working directory to a drive. The following makes the C:\ drive as the current working directory.C:`

os.chdir("C:\")
print(os.getcwd()) #output: 'C:\'

os.chdir("C:\") print(os.getcwd()) #output: 'C:\' In order to set the current directory to the parent directory use ”..” as the argument in the chdir() function. ".."``chdir()

os.chdir("C:\MyPythonProject")
print(os.getcwd()) #output: 'C:\MyPythonProject'
os.chdir("..")
print(os.getcwd()) #output: 'C:\'

`os.chdir(“C:\MyPythonProject”)

print(os.getcwd()) #output: ‘C:\MyPythonProject’ os.chdir(”..”) print(os.getcwd()) #output: ‘C:‘`

The rmdir() function in the OS module removes the specified directory either with an absolute or relative path. Note that, for a directory to be removed, it should be empty. rmdir()

import os
os.rmdir("C:\MyPythonProject")

`import os

os.rmdir(“C:\MyPythonProject”)` However, you can not remove the current working directory. To remove it, you must change the current working directory, as shown below.

import os
print(os.getcwd()) #output: 'C:\MyPythonProject'
os.rmdir("C:\MyPythonProject") #PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
os.chdir("..")
os.rmdir("MyPythonProject")

`import os

print(os.getcwd()) #output: ‘C:\MyPythonProject’

os.rmdir(“C:\MyPythonProject”) #PermissionError: [WinError 32] The process cannot access the file because it is being used by another process os.chdir(”..”) os.rmdir(“MyPythonProject”)Above, the MyPythonProject will not be removed because it is the current directory. We changed the current working directory to the parent directory using os.chdir("..") and then remove it using the rmdir() function.MyPythonProjectos.chdir("..")rmdir()`

The listdir() function returns the list of all files and directories in the specified directory. listdir()

import os
print(os.listdir("c:python37"))

`import os

print(os.listdir(“c:python37”))` If we don’t specify any directory, then list of files and directories in the current working directory will be returned.

import os
print(os.listdir()) #output: ['.config', '.dotnet', 'python']

`import os

print(os.listdir()) #output: [‘.config’, ‘.dotnet’, ‘python’]` Learn more about OS modules in Python docs. OS modules in Python docs