File Operations
Create a File
We are creating a file named test.txt
test.txt
using os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to create a file in the current directory.
open('test.txt', 'w').close()
open('test.txt', 'w').close()
Output:
C:\Users\username>python create_file.py
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 w
w
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.txt
test.txt
to new_test.txt
new_test.txt
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to rename a file in the current directory.
Syntax:
os.rename(src, dst)
os.rename(src, dst)
Example:
import os
os.rename('test.txt', 'new_test.txt')
import os
os.rename('test.txt', 'new_test.txt')
Output:
C:\Users\username>python rename_file.py
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.txt
new_test.txt
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to delete a file in the current directory.
Syntax:
os.remove(path)
os.remove(path)
Example:
import os
os.remove('new_test.txt')
import os
os.remove('new_test.txt')
Output:
C:\Users\username>python delete_file.py
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 test
test
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to create a directory in the current directory.
Syntax:
os.mkdir(path)
os.mkdir(path)
Example:
import os
os.mkdir('test')
import os
os.mkdir('test')
Output:
C:\Users\username>python create_directory.py
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 test
test
to new_test
new_test
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to rename a directory in the current directory.
Syntax:
os.rename(src, dst)
os.rename(src, dst)
Example:
import os
os.rename('test', 'new_test')
import os
os.rename('test', 'new_test')
Output:
C:\Users\username>python rename_directory.py
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_test
new_test
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to delete a directory in the current directory.
Syntax:
os.rmdir(path)
os.rmdir(path)
Example:
import os
os.rmdir('new_test')
import os
os.rmdir('new_test')
Output:
C:\Users\username>python delete_directory.py
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.txt
test.txt
to the directory test
test
using the shutil
shutil
module. The shutil
shutil
module provides functions for copying and moving files and directories. The shutil
shutil
module comes under Python’s standard utility modules. We are using the shutil
shutil
module to move a file in the current directory.
Syntax:
shutil.move(src, dst)
shutil.move(src, dst)
Example:
import shutil
shutil.move('test.txt', 'test')
import shutil
shutil.move('test.txt', 'test')
Output:
C:\Users\username>python move_file.py
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 test
test
to the directory new_test
new_test
using the shutil
shutil
module. The shutil
shutil
module provides functions for copying and moving files and directories. The shutil
shutil
module comes under Python’s standard utility modules. We are using the shutil
shutil
module to move a directory in the current directory.
Syntax:
shutil.move(src, dst)
shutil.move(src, dst)
Example:
import shutil
shutil.move('test', 'new_test')
import shutil
shutil.move('test', 'new_test')
Output:
C:\Users\username>python move_directory.py
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.txt
test.txt
to the directory test
test
using the shutil
shutil
module. The shutil
shutil
module provides functions for copying and moving files and directories. The shutil
shutil
module comes under Python’s standard utility modules. We are using the shutil
shutil
module to copy a file in the current directory.
Syntax:
shutil.copy(src, dst)
shutil.copy(src, dst)
Example:
import shutil
shutil.copy('test.txt', 'test')
import shutil
shutil.copy('test.txt', 'test')
Output:
C:\Users\username>python copy_file.py
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 test
test
to the directory new_test
new_test
using the shutil
shutil
module. The shutil
shutil
module provides functions for copying and moving files and directories. The shutil
shutil
module comes under Python’s standard utility modules. We are using the shutil
shutil
module to copy a directory in the current directory.
Syntax:
shutil.copytree(src, dst)
shutil.copytree(src, dst)
Example:
import shutil
shutil.copytree('test', 'new_test')
import shutil
shutil.copytree('test', 'new_test')
Output:
C:\Users\username>python copy_directory.py
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 os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to list the files and directories in the current directory.
Syntax:
os.listdir(path)
os.listdir(path)
Example:
import os
print(os.listdir())
import os
print(os.listdir())
Output:
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']
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.txt
test.txt
exists in the current directory using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to check if a file exists in the current directory.
Syntax:
os.path.exists(path)
os.path.exists(path)
Example:
import os
print(os.path.exists('test.txt'))
import os
print(os.path.exists('test.txt'))
Output:
C:\Users\username>python check_if_file_exists.py
True
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 test
test
exists in the current directory using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to check if a directory exists in the current directory.
Syntax:
os.path.exists(path)
os.path.exists(path)
Example:
import os
print(os.path.exists('test'))
import os
print(os.path.exists('test'))
Output:
C:\Users\username>python check_if_directory_exists.py
True
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 os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to get the current working directory.
Syntax:
os.getcwd()
os.getcwd()
Example:
import os
print(os.getcwd())
import os
print(os.getcwd())
Output:
C:\Users\username>python get_current_working_directory.py
C:\Users\username
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 test
test
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to change the current working directory.
Syntax:
os.chdir(path)
os.chdir(path)
Example:
import os
os.chdir('test')
import os
os.chdir('test')
Output:
C:\Users\username>python change_current_working_directory.py
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.txt
test.txt
using the os
os
module. The os
os
module provides functions for interacting with the operating system. The os
os
module comes under Python’s standard utility modules. We are using the os
os
module to get the size of a file.
Syntax:
os.path.getsize(path)
os.path.getsize(path)
Example:
import os
print(os.path.getsize('test.txt'))
import os
print(os.path.getsize('test.txt'))
Output:
C:\Users\username>python get_file_size.py
0
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 test
test
using the shutil
shutil
module. The shutil
shutil
module provides functions for copying and moving files and directories. The shutil
shutil
module comes under Python’s standard utility modules. We are using the shutil
shutil
module to get the size of a directory.
Syntax:
shutil.disk_usage(path)
shutil.disk_usage(path)
Example:
import shutil
print(shutil.disk_usage('test'))
import shutil
print(shutil.disk_usage('test'))
Output:
C:\Users\username>python get_directory_size.py
usage(total=97676207104, used=97676207104, free=0)
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 os
os
module and the official documentation of the shutil
shutil
module. For more tutorials, visit our Python Central Hub.
Was this page helpful?
Let us know how we did