Open In App

File System Manipulation in Python

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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!

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads