File Processing Operations

Last updated: May 25, 2026

File Processing Operations

Python os module provides methods that help you perform file-processing operations, such as renaming and deleting files. To use this module you need to import it first and then you can call any related functions.


`os.rename()`

The rename() method takes two arguments, the current filename and the new filename (to rename file).

Syntax:

os.rename(current_file_name, new_file_name)

Example:

import os
os.rename("sample.txt", "same.txt")

`os.mkdir()`

The mkdir() method takes one argument as the directory name that you want to create.

Syntax:

os.mkdir(directory name)

Example:

import os
os.mkdir("python")

`os.rmdir()`

The rmdir() method takes one argument as the directory name that you want to remove. This method is used to remove a directory.

Syntax:

os.rmdir(directory name)

Example:

import os
os.rmdir("python")

`os.chdir()`

The chdir() method takes one argument as the directory name which we want to change. This method is used to change directory.

Syntax:

os.chdir(newdir)

Example:

import os
# change directory to D
os.chdir("D")

`os.remove()`

The remove() method takes one argument, the filename that you want to remove. This method is used to remove a file.

Syntax:

os.remove(filename)

Example:

import os
# removes python.txt named file
os.remove("python.txt")

`os.getcwd()`

The getcwd() method takes zero arguments, it gives the current working directory.

Syntax:

os.getcwd()

Example:

import os
# It gives current working directory
os.getcwd()