File System Manipulation in Python
Last Updated :
23 Jul, 2025
File system manipulation in Python refers to the ability to perform various operations on files, such as creating, reading, writing, appending, renaming, and deleting. Python provides several built-in modules and functions that allow you to perform various file system operations. Python treats files differently based on whether they are text or binary. Each line of code in a text file includes a sequence of characters, terminated by an End of Line (EOL) character.
File System Manipulation in Python
Below are some examples by which we can understand File System Manipulation in Python:
- Reading the File
- Writing to the File
- Creating a Directory and Writing File
- Moving and Renaming Files
geeks.txt
Hello world
GeeksforGeeks
123
456
Reading the File
In this example, the below code opens a file named "geeks.txt" in read mode using the 'with' statement, ensuring proper file handling. It reads the content of the file and assigns it to the variable 'content'. Finally, it prints the content to the console.
Python3
# Open the file in read mode
with open("geeks.txt", "r") as file:
content = file.read()
print(content)
Output
Hello world
GeeksforGeeks
123
456
Writing to the File
In this example, below code opens a file named "geeks.txt" in write mode using the 'with' statement for proper file handling. It then writes the string "Python is amazing!" to the file, effectively replacing any previous content with this new text.
Python3
# Open the file in write mode
with open("geeks.txt", "w") as file:
file.write("Python is amazing!")
Output (geeks.txt)
Python is amazing!
Creating Directory & Writing File
In this example, below code imports the 'os' module for operating system functionality. It creates a directory named "my_directory" using 'os.mkdir()'. Then, it changes the current working directory to "my_directory" using 'os.chdir()'. Inside the directory, it creates a file named "my_file.txt" and writes "Hello, world!" to it.
Python3
import os
# Create a directory
os.mkdir("my_directory")
# Change to the new directory
os.chdir("my_directory")
# Create a file and write to it
with open("my_file.txt", "w") as file:
file.write("Hello, world!")
# Verify the file and its content
print(os.listdir())
# Output: ['my_file.txt']
Output
['my_file.txt']
my_directory/my_file.txt:
Hello, world!
Moving and Renaming Files
In this example, below code imports the 'shutil' and 'os' modules for file operations. It creates a file named "my_file.txt" with the content "Hello, world!". Then, it moves and renames the file to "new_directory/my_new_file.txt".
Python3
import shutil
import os
# Create a file
with open("my_file.txt", "w") as file:
file.write("Hello, world!")
# Move and rename the file
shutil.move("my_file.txt", "new_directory/my_new_file.txt")
# Verify the new file location and name
print(os.listdir("new_directory"))
Output
['my_file.txt']
my_file.txt:
Hello, world!
Similar Reads
Append Text or Lines to a File in Python Appending text or lines to a file is a common operation in programming, especially when you want to add new information to an existing file without overwriting its content. In Python, this task is made simple with built-in functions that allow you to open a file and append data to it. In this tutori
3 min read
Print the Content of a Txt File in Python Python provides a straightforward way to read and print the contents of a .txt file. Whether you are a beginner or an experienced developer, understanding how to work with file operations in Python is essential. In this article, we will explore some simple code examples to help you print the content
3 min read
Replace Multiple Lines From A File Using Python In Python, replacing multiple lines in a file consists of updating specific contents within a text file. This can be done using various modules and their associated functions. In this article, we will explore three different approaches along with the practical implementation of each approach in term
3 min read
Python Program to Replace Text in a File In this article, we are going to replace Text in a File using Python. Replacing Text could be either erasing the entire content of the file and replacing it with new text or it could mean modifying only specific words or sentences within the existing text.Method 1: Removing all text and write new te
3 min read
Check If a Text File Empty in Python Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.Check if a
4 min read
Check If A File is Writable in Python When it comes to Python programming it is essential to work with files. One important aspect is making sure that a file can be written before you try to make any changes. In this article, we will see how we can check if a file is writable in Python. Check If a File Is Writable in PythonBelow are som
3 min read