Python Reading and Writing Files

Read From File

To read from the file, you can use the open function to open the file. It opens it and returns a file object that users can use to read or modify the content of that file. The syntax is open(file_name, mode), the file_name is the name of the file you want to interact with, and the mode could be any of these:

  • r read mode
  • w write mode (Overwrites existing file)
  • a append to the end mode
  • b binary mod
    • There are other modes, but these are commonly used

File Content

Test1
Test2

Example

temp_file = open(“test_1.txt”, “r”) # Open the file “test_1.txt” in read mode (“r”)
print(temp_file.read()) # Read the entire contents of the file and print it
temp_file.close() # Close the file to free system resources

temp_file = open("test_1.txt","r")

print(temp_file.read())
temp_file.close()

Result

Test1
Test2

Read From File (Line by Line)

You can use the .readline method to read line by line

File Content

Test1
Test2

Example

temp_file = open(“test_1.txt”) # Open the file “test_1.txt” in read mode (default mode is “r”)
for line in temp_file.readlines(): # Read all lines into a list and iterate through each line
    print(line, end=””) # Print each line without adding extra newlines (end=””)
temp_file.close() # Close the file to free system resources

temp_file = open("test_1.txt")
for line in temp_file.readlines():
print(line, end="")

temp_file.close()

Result

Test1
Test2

Or, you can use the .readlines method

Example

temp_file = open(“test_1.txt”) # Open the file “test_1.txt” in read mode (default “r”)
lines = temp_file.readlines() # Read all lines into a list called ‘lines’
for line in lines: # Iterate through each line in the list
    print(line, end=””) # Print each line without adding extra newlines
temp_file.close() # Close the file to free system resources

temp_file = open("test_1.txt")

lines = temp_file.readlines()
for line in lines:
print(line, end="")

temp_file.close()

Write to File

To write, you can use the .write method

Example

temp_file = open(“test_1.txt”, “w”) # Open the file in write mode (“w”); creates the file if it doesn’t exist, or overwrites it if it exists
temp_file.write(“Test\n”) # Write the string “Test” followed by a newline to the file
temp_file.close() # Close the file to save changes and free resources
temp_file = open(“test_1.txt”, “r”) # Reopen the file in read mode (“r”)
print(temp_file.read()) # Read the entire file contents and print that
temp_file.close() # Close the file after reading

temp_file = open("test_1.txt","w")
temp_file.write("Test\n")
temp_file.close()

temp_file = open("test_1.txt","r")
print(temp_file.read())
temp_file.close()

Result

Test

Write to File (With User Input)

You can ask the user for input, then save that to a file

User Input

Hello World!

Example

temp_file = open(“test_1.txt”, “a+”) # Open the file in append and read mode (“a+”); creates file if it doesn’t exist
temp_user_input = input(“Enter text: “) # Prompt the user to enter text
temp_file.write(temp_user_input) # Append the user’s input to the end of the file
temp_file.close() # Close the file to save changes
temp_file = open(“test_1.txt”, “r”) # Reopen the file in read mode
print(temp_file.read()) # Read and print the entire contents of the file
temp_file.close() # Close the file after reading

temp_file = open("test_1.txt","a+")
temp_user_input = input("Enter text: ")
temp_file.write(temp_user_input)
temp_file.close()

temp_file = open("test_1.txt","r")
print(temp_file.read())
temp_file.close()

Result

Hello World!

Read\Write Without Close Method

The .close method is used to close the opened file (It’s a good practice to do that). If you do not want to use that, then use the with the statement, which will automatically close it when flow control leaves the with block

File Content

Test1
Test2

Example

with open(“test_1.txt”, “r”) as f: # Open the file “test_1.txt” in read mode; ‘with’ ensures it will be automatically closed
    print(f.read()) # Read the entire file content and print it

with open("test_1.txt","r") as f:
print(f.read())

Result

Test1
Test2

Remove a File

There are different ways to delete a file, one of them is the use the remove function from the Miscellaneous operating system interfaces module (You need to import it first using import os).

User Input

Hello World!

Example

import os # Import the os module for interacting with the operating system
os.remove(“test_1.txt”) # Delete the file “test_1.txt” from the filesystem

import os
os.remove("test_1.txt")