Handling Files in Python

published on: | by cindy In category: Python

Sometimes you may want to store data in secondary storage such a file to access later. A good use case is logging system errors on the computer.

Python has two types of files.

  1. Text Files. A structured sequence of lines and characters. Each line can be terminated using an end of a line such as the (/n) character
  2. Binary Files. Return Bytes. Can only be processed by the app that understands its structure.

we will focus on the text files. We will focus on how to create, read and write on the file. To perform any action on a file, we must tell python which file we are going to work with and what we want to do with it which is optional. To explain how python handle files consider the common analogy of opening a notebook.

Before you can do anything to the notebook you must

  1. Open the book.
  2. Do what you want. Read, write or append more writings to the existing file

you must know the name of the file to find it. You have to open it to write on it, append something on it and close it when you are done.

Access Modes

The access mode defines the kind of operation to perform on the file after it is opened. This is optional. If not given defaults to read mode.

  1. Read Only('r') This is the default mode. It opens a file for reading.
  2. Write Only('w') If the file does not exist, it creates a new file. If the file exists it truncates the file. (overwrites)
  3. Read and Write('r+').This will open a file for reading and writing (updating)
  4. Append ('a') If the file does not exist, it creates a new file.
  5. Create('x') Creates a new file. If the file already exists, the operation fails. Introduced in Python 3.3
  6. Append and write ('a+')

Open File

use open() method to open the file and will return a file object if found. It takes a minimum of one argument which is the name of the file(don't forget to include the file extension) as a string .it also takes the access mode as the second argument. The open() asks the operating system to find the file by name and make sure the file exists. If successful, i.e if the requested file exists and you have the proper permissions the Os gives us the file handler. The handler is not the actual data returned but it 'handles ' how we read data from the file or write to it.

you can explicitly tell python to create the file using 'x' mode if it doesn't exist already otherwise will return an error.

we are going to create a log.txt file

# open the file and read  from it.creates a file if it doesnt exist  
logs=open('log.txt', 'x')  
# close the file once done  
logs.close()

Every time you create a file remember to close it after the operation is done.

Read data in the file

  1. read(size): returns the read byte as a string. If the number of bytes is not specified it reads and returns the entire file. The size is optional. In case the size is negative or omitted, the entire file will be read and returned.

!!! achiengbytes "

Importance of specifying the size

"

​ Imagine a situation where the content you need from the file is maybe 10 bytes, the size of the file itself is larger than your machine memory. You will definitely have a problem returning the whole content of that file

Let me make the code more readable. Put the file name and the access mode in a variable. This time lets read from the file we just created

python fileName= log.txt acessMode ='r' logs = open(fileName, acessMode) print(logs.read()) # returns an entire string #specify 5 bytes print(logs.read(5)) # reurs a sting with 5 bytes

  1. readline() Reads and return one line from the file.
  2. readlines()reads line by line

Write to the file

We can write the following strings to the file we created in the example above. Simply use the write() method.

The write method truncates and overwrites

Add these lines before closing the files.

logs.write('Hello Cindy! \n')   
logs.write('Hello Tess! \n')

Writing to logs.txt after closing the file will raise this error ValueError: I/O operation on closed file. Also, to make our code even more readable, I'll define the mode Since we will be using various access mode let us use 'mode' for readability

fileName= log.txt  
# comment the acessMode variable  
# acessMode ='r'  
# define the modes  
WRITE = "w"  
APPEND = "a"  
READ = "r"  
logs = open(fileName, mode=WRITE)  
logs.write('Hello Cindy! \n')  
logs.write('Hello Tess! \n')  
# close the file once done  
logs.close()

Do you remember the range() function we learned in lists? It helps us create a range of numbers Let's create a range of numbers 1-10 and write it to our files

fileName= log.txt  
#acessMode ='r'   
WRITE = "w"  
APPEND = "a"  
READ = "r"  
logs = open(fileName, mode=WRITE)  
# write a range of number from 1-10  
for i in range(10):  
 files.write("This is line %d\r\n" % i)  
# close the file once done  
logs.close()

Append to the file

Getting more fun. If you simply want to append some data to the file, use append since write will truncate the data in the file.

How about we take users to input this time and append it to the file?

...  
data = input("Enter a list of values")  
logs = open(fileName, mode=APPEND)  
files.write(data)  
print('list sucessfully written')  
# remember to close your file after opening  
files.close()

Creating files using with keyword

It is a good practice to use the keyword with when dealing with file objects.

It is advantageous because:

  1. The file will be properly closed after its suites finish execution even if an exception is raised at some point
  2. Much shorter than using try-finally blocks

Closing the file

​ If you do not use the with keyword then you should call the close() function. This will free up any system resources used by it.

​ If you do not explicitly close the file, the python garbage collector will destroy the object and close the file for. The downside is your file may remain open for a longer duration.

​ Also, different python implementation will do the clean up at different times