Working With Files In Python

1. READING TEXT FROM A FILE

In this section, we will learn different methods to load a file into the program.

1.1. Reading an Entire File

In the following example, we open a file (.txt format) that contains first paragraph of the book,Alice in the Wonderland. You can download the whole text here After opening, we will print its content:

# storing the file path in a variable
file_name = "alice_3p.txt"
# opening the file as file_object
with open(file_name) as file_object:
    # saving the content of file
    content = file_object.read()
    # printing 'content'
    print(content)
  • first, we defined the path to the file and stored it in the variable file_name

  • open() function needs one argument: the name of the file we want to open. The open() function returns an object representing the file. In our example, open(file_name) returns an object representing alice_3p.txt. Python stores this in file_object, which weโ€™ll work with later in the program

  • notice how we call open() in this program but not close() because the keyword with closes the file automatically once it is no longer needed

  • we used the read() method to read the entire contents of the file and store it as one long string in the variable,content

  • when we print the value of content, we get the entire text file back:

Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do. Once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, "and what is the use of a book," thought Alice, "without pictures or
conversations?"

File path:

  • Relative File Path: In the above example, alice_3p.txt file is present in the same folder from where this program is stored so we provided a relative file path โ€” a relative file path tells Python to look for the specified file in a location relative to the directory where the currently running program file is stored.

  • Absolute File Path: We can also tell Python the exact location of the file iregardless of where the program thatโ€™s being executed is stored. This is called an absolute file path

1.2. Reading Line by Line

We can use a for loop on the file_object to examine each line from the file:

# storing the file path in a variable
file_name = "alice_3p.txt"
# opening the file as file_object
with open(file_name) as file_object:
    # using for loop on file_object
    for line in file_object:
        # printing each line
        print(line)

Here is the output:

Alice was beginning to get very tired of sitting by her sister on the

bank, and of having nothing to do. Once or twice she had peeped into the

book her sister was reading, but it had no pictures or conversations in

it, "and what is the use of a book," thought Alice, "without pictures or

conversations?"

These blank lines appear because an invisible newline character() is at the end of each line in the text file. We can use the strip() method to remove the blank lines

1.3. Making a List of Lines from a File

In the above example, we did use for loop to print each line in the file but what if we need this list of lines (content of file) outside the with block? When we use with, the file object returned by open() is only available inside the with block that contains it. If we want to access the fileโ€™s contents outside the with block, we can store the content in a list inside the with block and then work with that list later:

file_name = 'material/chapter_10/pi_digits.txt'
with open(file_name) as file_object:
    content_lines = file_object.readlines()
    print(content_lines)

for content_line in content_lines:
    print(content_line)
  • Notice that we used the readlines() method to store the text lines as a list as compared to read() method, which stores the entire content as a string.

  • secondly, we print the list as whole, inside the with block, AND print each item inside the list using a for loop outside the with block Here is the output:

['Alice was beginning to get very tired of sitting by her sister on the\n', 'bank, and of having nothing to do. Once or twice she had peeped into the\n', 'book her sister was reading, but it had no pictures or conversations in\n', 'it, "and what is the use of a book," thought Alice, "without pictures or\n', 'conversations?"']

Printing outside the `with` block:

Alice was beginning to get very tired of sitting by her sister on the

bank, and of having nothing to do. Once or twice she had peeped into the

book her sister was reading, but it had no pictures or conversations in

it, "and what is the use of a book," thought Alice, "without pictures or

conversations?"
  • now you can see why there is a blank line โ€” the presence of new line character () at end of each line, except the last one.

NOTE: When Python reads from a text file, it interprets all text in the file as a string. Therefore, if our text file contains numerical data, we have to convert it to an integer using the int() function or convert it to a float using the float() function.

1.4. Working with Fileโ€™s Contents

Example 1: Wrapping content into single line

Let suppose that after we store each line of the text file inside a list using readlines() method, we want to display the content in single line rather the line breaks included.

---

# empty string to hold the value
single_line = ""
for content_line in content_lines:
    # appending the lines, strip() to remove /n
    single_line += f" {content_line.strip()}"

# print the end result
print(single_line)

Output would be:

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do. Once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice, "without pictures or conversations?"

we have used the strip() method to remove all the blank lines

Example 2: Checking if your birthdate is within 1 million pi digits

In this example, we have used a file that contains the pi first 1 million digits after decimal place. To work with this in interesting manner, letโ€™s write program to check whether someone birthday is within these 1 million digits.

# file path
file_name = "pi1000000.txt"
# opening file and saving its content
with open(file_name) as file_object:
    lines = file_object.readlines()

# removing all spaces, new lines
pi_million = ''
for line in lines:
    pi_million += line.strip()

# getting user input, birthdate
birthdate =input("Enter birthdate ddmmyy: ")
# checking if the birthdate inside pi_milliom
if birthday in pi_million:
    print(f"Your birthdate, [{birthdate}] is within million digits of Pi")
else:
    print("Alas, your birthdate is not within million digits of Pi ")
Enter birthdate ddmmyy: 101090
Your birthdate, [101090] is within million digits of Pi

Example 3: Replacing a word with another

Let suppose, we have text file that contains the topics that we have learned in Python (one topic on each line, ending with โ€ฆ in Python) We can use the replace() method to replace all instances of Python with Py.

file_path = "python_los.txt"
with open(file_path) as file_object:
    string_lines = file_object.readlines()
    print(string_lines)

single_line_py = ''
for string_line in string_lines:
    # replacing 'Python' with 'Py' 
    # using .replace method on string
    single_line_py += string_line.replace('Python', 'Py')

print(single_line_py)

Output:

['List in Python\n', 'Dictionary in Python\n', 'Functions in Python\n', 'Classes in Python']
List in Py
Dictionary in Py
Functions in Py
Classes in Py

2. WRITING TEXT TO A FILE

To write any text to a file, we need to use open() with a two arguments โ€” the first argument is still the name of the file where we want to write and the second argument, w tells Python that we want to open the file in write mode.

We can open a file in read mode r, write mode w, append mode a, or mode that allows to both read and write to the file r+ If we omit the mode argument, Python opens the file in read-only mode by default.

NOTE: Python write strings to a text file, by default. If we want to store numerical data in a text file, we will have to convert the data to numerical format first using the int() function

2.1. Writing to an Empty File

Let suppose we want to create a new file welcome.txt and store a single string, Welcome to all guests The open() function automatically creates the file weโ€™re writing to if it doesnโ€™t already exist.

filename = "welcome.txt"
message = "Welcome to all guests"

# writing to file
with open(filename, 'w') as file_object:
    # .write method on file_object
    file_object.write(message)

# reading and printing the content of file    
with open(filename) as file_object:
    contents = file_object.read()
    print(contents) 
Welcome to all guests

Writing multiple lines

We can write() as many lines as we wish. For example:

filename = "welcome.txt"
message1 = "Welcome to all guests\n"
message2 = "Lunch will be at Lobby A on 3rd floor\n"

# writing to file
with open(filename, 'w') as file_object:
    # .write method on file_object
    file_object.write(message1)
    file_object.write(message2)

# reading and printing the content of file    
with open(filename) as file_object:
    contents = file_object.read()
    print(contents) 
Welcome to all guests
Lunch will be at Lobby A on 3rd floor

2.2. Appending to a File

When we open a file in append a mode, Python doesnโ€™t erase the file before returning the file object. Therefore, all content we append will be added at the end of the file. Besides, just we studied above, if the file doesnโ€™t exist, Python will create an empty file for us, before appending the content. Letโ€™s append another line to the above file, welcome.txt

# string to append
message_append = "Please return to the lecture hall before 1300 hours"

# opening file in append mode
with open(filename, 'a') as file_object:
    # appending the while with new content
    file_object.write(message_append)

#reading and printing the content of file    
with open(filename) as file_object:
    contents = file_object.read()
    print(contents)
Welcome to all guests
Lunch will be at Lobby A on 3rd floor
Please return to the lecture hall before 1300 hours

3. JSON DATA

In the following section we will work with json module. To read more about the json data structure, please visit this link

3.1. Storing and Retrieving JSON Data

In Python, we store data using json.dump() , and then load the dat into program using json.load()

a. Store the data

The json.dump() function takes two arguments: a piece of data to store and a location where to store the data. Here is an example:

# importing json module
import json

# data from user input
username = input("Please enter desired username: ")
# where to store the data
filename = 'username.json'

# anatomy of saving the json data
with open(filename, 'w') as file_object:
    json.dump(username, file_object)
    print(f"Following username has been stored: {username}")
Please enter desired username: dexter
Following username has been stored: dexter
  • we first import the json module, to make the program work with json data type

  • then we ask the user to provide the username and store it inside the variable username

  • then we provide the location of file path to store the data, filename

  • then we open the file in write mode w and store it inside file_object

  • then using json.dump() function we store the data in the file. The first argument is location to store the data and second argument is data itself

b. Retrieve the data

We use the json.load() to load the data stored inside given file back into program

filename = 'username.json'

# anatomy of loading the json data
with open(filename) as file_object:
    username = json.load(file_object)
    print("Welcome back " + username)
Welcome back dexter

Last updated