Skip to content

File Operations

Create a File

We are creating a file named test.txttest.txt using osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to create a file in the current directory.

create_file.py
open('test.txt', 'w').close()
create_file.py
open('test.txt', 'w').close()

Output:

command
C:\Users\username>python create_file.py
command
C:\Users\username>python create_file.py

In the above example, we are using the open()open() function to create a file. The open()open() function takes two arguments. The first argument is the name of the file and the second argument is the mode. The mode is used to specify the purpose of opening the file. The ww mode is used to open the file for writing. The open()open() function returns a file object. We are using the close()close() method to close the file object.

Rename a File

We are renaming the file test.txttest.txt to new_test.txtnew_test.txt using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to rename a file in the current directory.

Syntax:

rename_file.py
os.rename(src, dst)
rename_file.py
os.rename(src, dst)

Example:

rename_file.py
import os
 
os.rename('test.txt', 'new_test.txt')
rename_file.py
import os
 
os.rename('test.txt', 'new_test.txt')

Output:

command
C:\Users\username>python rename_file.py
command
C:\Users\username>python rename_file.py

In the above example, we are using the rename()rename() function to rename a file. The rename()rename() function takes two arguments. The first argument is the old name of the file and the second argument is the new name of the file.

Delete a File

We are deleting the file new_test.txtnew_test.txt using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to delete a file in the current directory.

Syntax:

delete_file.py
os.remove(path)
delete_file.py
os.remove(path)

Example:

delete_file.py
import os
 
os.remove('new_test.txt')
delete_file.py
import os
 
os.remove('new_test.txt')

Output:

command
C:\Users\username>python delete_file.py
command
C:\Users\username>python delete_file.py

In the above example, we are using the remove()remove() function to delete a file. The remove()remove() function takes one argument. The argument is the name of the file to be deleted.

Create a Directory

We are creating a directory named testtest using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to create a directory in the current directory.

Syntax:

create_directory.py
os.mkdir(path)
create_directory.py
os.mkdir(path)

Example:

create_directory.py
import os
 
os.mkdir('test')
create_directory.py
import os
 
os.mkdir('test')

Output:

command
C:\Users\username>python create_directory.py
command
C:\Users\username>python create_directory.py

In the above example, we are using the mkdir()mkdir() function to create a directory. The mkdir()mkdir() function takes one argument. The argument is the name of the directory to be created.

Rename a Directory

We are renaming the directory testtest to new_testnew_test using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to rename a directory in the current directory.

Syntax:

rename_directory.py
os.rename(src, dst)
rename_directory.py
os.rename(src, dst)

Example:

rename_directory.py
import os
 
os.rename('test', 'new_test')
rename_directory.py
import os
 
os.rename('test', 'new_test')

Output:

command
C:\Users\username>python rename_directory.py
command
C:\Users\username>python rename_directory.py

In the above example, we are using the rename()rename() function to rename a directory. The rename()rename() function takes two arguments. The first argument is the old name of the directory and the second argument is the new name of the directory.

Delete a Directory

We are deleting the directory new_testnew_test using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to delete a directory in the current directory.

Syntax:

delete_directory.py
os.rmdir(path)
delete_directory.py
os.rmdir(path)

Example:

delete_directory.py
import os
 
os.rmdir('new_test')
delete_directory.py
import os
 
os.rmdir('new_test')

Output:

command
C:\Users\username>python delete_directory.py
command
C:\Users\username>python delete_directory.py

In the above example, we are using the rmdir()rmdir() function to delete a directory. The rmdir()rmdir() function takes one argument. The argument is the name of the directory to be deleted.

Move a File

We are moving the file test.txttest.txt to the directory testtest using the shutilshutil module. The shutilshutil module provides functions for copying and moving files and directories. The shutilshutil module comes under Python’s standard utility modules. We are using the shutilshutil module to move a file in the current directory.

Syntax:

move_file.py
shutil.move(src, dst)
move_file.py
shutil.move(src, dst)

Example:

move_file.py
import shutil
 
shutil.move('test.txt', 'test')
move_file.py
import shutil
 
shutil.move('test.txt', 'test')

Output:

command
C:\Users\username>python move_file.py
command
C:\Users\username>python move_file.py

In the above example, we are using the move()move() function to move a file. The move()move() function takes two arguments. The first argument is the name of the file to be moved and the second argument is the name of the directory to which the file is to be moved.

Move a Directory

We are moving the directory testtest to the directory new_testnew_test using the shutilshutil module. The shutilshutil module provides functions for copying and moving files and directories. The shutilshutil module comes under Python’s standard utility modules. We are using the shutilshutil module to move a directory in the current directory.

Syntax:

move_directory.py
shutil.move(src, dst)
move_directory.py
shutil.move(src, dst)

Example:

move_directory.py
import shutil
 
shutil.move('test', 'new_test')
move_directory.py
import shutil
 
shutil.move('test', 'new_test')

Output:

command
C:\Users\username>python move_directory.py
command
C:\Users\username>python move_directory.py

In the above example, we are using the move()move() function to move a directory. The move()move() function takes two arguments. The first argument is the name of the directory to be moved and the second argument is the name of the directory to which the directory is to be moved.

Copy a File

We are copying the file test.txttest.txt to the directory testtest using the shutilshutil module. The shutilshutil module provides functions for copying and moving files and directories. The shutilshutil module comes under Python’s standard utility modules. We are using the shutilshutil module to copy a file in the current directory.

Syntax:

copy_file.py
shutil.copy(src, dst)
copy_file.py
shutil.copy(src, dst)

Example:

copy_file.py
import shutil
 
shutil.copy('test.txt', 'test')
copy_file.py
import shutil
 
shutil.copy('test.txt', 'test')

Output:

command
C:\Users\username>python copy_file.py
command
C:\Users\username>python copy_file.py

In the above example, we are using the copy()copy() function to copy a file. The copy()copy() function takes two arguments. The first argument is the name of the file to be copied and the second argument is the name of the directory to which the file is to be copied.

Copy a Directory

We are copying the directory testtest to the directory new_testnew_test using the shutilshutil module. The shutilshutil module provides functions for copying and moving files and directories. The shutilshutil module comes under Python’s standard utility modules. We are using the shutilshutil module to copy a directory in the current directory.

Syntax:

copy_directory.py
shutil.copytree(src, dst)
copy_directory.py
shutil.copytree(src, dst)

Example:

copy_directory.py
import shutil
 
shutil.copytree('test', 'new_test')
copy_directory.py
import shutil
 
shutil.copytree('test', 'new_test')

Output:

command
C:\Users\username>python copy_directory.py
command
C:\Users\username>python copy_directory.py

In the above example, we are using the copytree()copytree() function to copy a directory. The copytree()copytree() function takes two arguments. The first argument is the name of the directory to be copied and the second argument is the name of the directory to which the directory is to be copied.

List Files and Directories

We are listing the files and directories in the current directory using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to list the files and directories in the current directory.

Syntax:

list_files_and_directories.py
os.listdir(path)
list_files_and_directories.py
os.listdir(path)

Example:

list_files_and_directories.py
import os
 
print(os.listdir())
list_files_and_directories.py
import os
 
print(os.listdir())

Output:

command
C:\Users\username>python list_files_and_directories.py
['append.py', 'create_directory.py', 'create_file.py', 'delete_directory.py', 'delete_file.py', 'File Operations.md', 'File%20Operations.md', 'move_directory.py', 'move_file.py', 'rename_directory.py', 'rename_file.py']
command
C:\Users\username>python list_files_and_directories.py
['append.py', 'create_directory.py', 'create_file.py', 'delete_directory.py', 'delete_file.py', 'File Operations.md', 'File%20Operations.md', 'move_directory.py', 'move_file.py', 'rename_directory.py', 'rename_file.py']

In the above example, we are using the listdir()listdir() function to list the files and directories in the current directory. The listdir()listdir() function takes one argument. The argument is the name of the directory whose files and directories are to be listed. If no argument is passed to the listdir()listdir() function, then it lists the files and directories in the current directory.

Check if a File Exists

We are checking if the file test.txttest.txt exists in the current directory using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to check if a file exists in the current directory.

Syntax:

check_if_file_exists.py
os.path.exists(path)
check_if_file_exists.py
os.path.exists(path)

Example:

check_if_file_exists.py
import os
 
print(os.path.exists('test.txt'))
check_if_file_exists.py
import os
 
print(os.path.exists('test.txt'))

Output:

command
C:\Users\username>python check_if_file_exists.py
True
command
C:\Users\username>python check_if_file_exists.py
True

In the above example, we are using the exists()exists() function to check if a file exists. The exists()exists() function takes one argument. The argument is the name of the file whose existence is to be checked.

Check if a Directory Exists

We are checking if the directory testtest exists in the current directory using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to check if a directory exists in the current directory.

Syntax:

check_if_directory_exists.py
os.path.exists(path)
check_if_directory_exists.py
os.path.exists(path)

Example:

check_if_directory_exists.py
import os
 
print(os.path.exists('test'))
check_if_directory_exists.py
import os
 
print(os.path.exists('test'))

Output:

command
C:\Users\username>python check_if_directory_exists.py
True
command
C:\Users\username>python check_if_directory_exists.py
True

In the above example, we are using the exists()exists() function to check if a directory exists. The exists()exists() function takes one argument. The argument is the name of the directory whose existence is to be checked.

Get the Current Working Directory

We are getting the current working directory using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to get the current working directory.

Syntax:

get_current_working_directory.py
os.getcwd()
get_current_working_directory.py
os.getcwd()

Example:

get_current_working_directory.py
import os
 
print(os.getcwd())
get_current_working_directory.py
import os
 
print(os.getcwd())

Output:

command
C:\Users\username>python get_current_working_directory.py
C:\Users\username
command
C:\Users\username>python get_current_working_directory.py
C:\Users\username

In the above example, we are using the getcwd()getcwd() function to get the current working directory. The getcwd()getcwd() function takes no argument.

Change the Current Working Directory

We are changing the current working directory to the directory testtest using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to change the current working directory.

Syntax:

change_current_working_directory.py
os.chdir(path)
change_current_working_directory.py
os.chdir(path)

Example:

change_current_working_directory.py
import os
 
os.chdir('test')
change_current_working_directory.py
import os
 
os.chdir('test')

Output:

command
C:\Users\username>python change_current_working_directory.py
command
C:\Users\username>python change_current_working_directory.py

In the above example, we are using the chdir()chdir() function to change the current working directory. The chdir()chdir() function takes one argument. The argument is the name of the directory to which the current working directory is to be changed.

Get the Size of a File

We are getting the size of the file test.txttest.txt using the osos module. The osos module provides functions for interacting with the operating system. The osos module comes under Python’s standard utility modules. We are using the osos module to get the size of a file.

Syntax:

get_file_size.py
os.path.getsize(path)
get_file_size.py
os.path.getsize(path)

Example:

get_file_size.py
import os
 
print(os.path.getsize('test.txt'))
get_file_size.py
import os
 
print(os.path.getsize('test.txt'))

Output:

command
C:\Users\username>python get_file_size.py
0
command
C:\Users\username>python get_file_size.py
0

In the above example, we are using the getsize()getsize() function to get the size of a file. The getsize()getsize() function takes one argument. The argument is the name of the file whose size is to be obtained.

Get the Size of a Directory

We are getting the size of the directory testtest using the shutilshutil module. The shutilshutil module provides functions for copying and moving files and directories. The shutilshutil module comes under Python’s standard utility modules. We are using the shutilshutil module to get the size of a directory.

Syntax:

get_directory_size.py
shutil.disk_usage(path)
get_directory_size.py
shutil.disk_usage(path)

Example:

get_directory_size.py
import shutil
 
print(shutil.disk_usage('test'))
get_directory_size.py
import shutil
 
print(shutil.disk_usage('test'))

Output:

command
C:\Users\username>python get_directory_size.py
usage(total=97676207104, used=97676207104, free=0)
command
C:\Users\username>python get_directory_size.py
usage(total=97676207104, used=97676207104, free=0)

In the above example, we are using the disk_usage()disk_usage() function to get the size of a directory. The disk_usage()disk_usage() function takes one argument. The argument is the name of the directory whose size is to be obtained.

Conclusion

In this tutorial, we learned how to perform file operations in Python. We learned how to create, rename, delete, and move files and directories. We also learned how to list files and directories, check if a file or directory exists, get the current working directory, change the current working directory, and get the size of a file or directory. Now you can perform file operations in Python. For more information, visit the official documentation of the osos module and the official documentation of the shutilshutil module. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did