LearninBits

How to Read and Write Files in Python: A Quick Guide

Reading from and writing to files is a common task in programming. Luckily, Python makes this simple! In this quick guide, we’ll show you the basics of handling files in Python. Let’s dive in!

1. Opening a File

Before you can read or write, you need to open the file. Python has a built-in function called open() for this.

# opening a file
file = open('my_file.txt', 'r')
  • 'my_file.txt': The name of your file.
  • 'r': The mode in which to open the file. 'r' stands for “read”.

Other common modes are:

  • 'w': Write (overwrites the file).
  • 'a': Append (adds to the end of the file).

2. Reading from a File

Once a file is open, you can read its content. Here’s how:

Read the Whole File:

# reading from a file
content = file.read()
print(content)

Read Line by Line:

# read line by line
for line in file:
    print(line)

3. Writing to a File

To write data to a file, first open it in write or append mode:

# writing to a file
file = open('my_file.txt', 'w')
file.write('Hello, LearnInBits readers!')

This will write “Hello, LearnInBits readers!” to my_file.txt.

4. Closing the File

After reading or writing, always close your file. It’s a good habit.

# closing the file
file.close()

5. Using ‘with’ for Easy File Handling

Python has a neat trick called with that automatically closes the file for you:

# using `with` for easy file handling
with open('my_file.txt', 'r') as file:
    content = file.read()
    print(content)

This way, you don’t have to remember to use file.close()!

Summary of the code above
# =====================
# Opening a File
# =====================

# Open a file in read mode
file = open('my_file.txt', 'r')

# Open a file in write mode (overwrites the file if it exists)
file = open('my_file.txt', 'w')

# Open a file in append mode (adds to the end if file exists)
file = open('my_file.txt', 'a')

# =====================
# Reading from a File
# =====================

# Read the entire content of the file
content = file.read()
print(content)

# Reset the file pointer to the beginning
file.seek(0)

# Read the file content line by line
for line in file:
    print(line)

# =====================
# Writing to a File
# =====================

# Open the file in write mode
file = open('my_file.txt', 'w')

# Write a string to the file
file.write('Hello, LearnInBits readers!')

# =====================
# Closing the File
# =====================

# Always close the file after operations
file.close()

# =====================
# Using 'with' for Easy File Handling
# =====================

# The file is automatically closed at the end of the 'with' block
with open('my_file.txt', 'r') as file:
    content = file.read()
    print(content)

Key Takeaways:

  • Open a file with open().
  • Always close your file with close().
  • Use 'r' to read, 'w' to write, and 'a' to append.
  • For hassle-free file handling, use the with statement.

And that’s it! In just a few minutes, you’ve learned the essentials of reading and writing files in Python. Happy coding!

Remember, practice makes perfect. Try reading from and writing to some sample files on your computer to reinforce what you’ve learned. If you have questions or want more bite-sized tutorials, don’t hesitate to drop a comment or follow us on Twitter!

Leave a Reply

Layer 1