A Complete Guide to Python File Handling: Data Manipulation Made Simple

A Complete Guide to Python File Handling: Data Manipulation Made Simple

Exploring  the File Handling Capabilities of Python

While developing applications there are instances where writing or reading from a file will be handy to enable proper functionalities since the RAM is volatile (holds data temporarily) when the program is executed. In this article we'll cover in-depth about File handling in Python programming. Operations such file mode, file object, functions etc. will be covered extensively.

💡
Already familiar with OOP in Python or New to Python? Check out the previous comprehensive article on Python Functions titled: A Comprehensive Guide to Understanding Object-Oriented Programming (OOP) in Python in this Python series.

File Handling

The process of creating, reading, modifying or deleting file or it's content is called file handling.

File Handling in Python

Python provides several function for manipulating files and directories (folder). The Text mode and Binary mode are the two type of file handling in Python. Operations for text file uses a default value of "t" while binary files uses "b"

Example of Binary files:

  • Document files (.pdf, .doc, .xls, .docx etc.)

  • Image files (.png, .jpg, .gif, jpeg, .bmp etc.)

  • Video files (.mp4, .3gp, .mkv, .avi, .mov etc.)

  • Audio files (.mp3, .wav, .mka, .aac etc.)

  • Database files (.mdb, .accde, .frm, .sqlite etc.), archive files ( .zip, .rar, .iso, .7z etc.)

  • Executable files: .exe, .dll, .class, etc.

Example Text files:

  • Web standards: html, XML, CSS, JSON etc.

  • Source code: c, app, js, py, java etc.

  • Documents: txt, tex, RTF etc.

  • Tabular data: csv, tsv etc.

  • Configuration: ini, cfg, reg etc.

Four major operations in Python File handling

  • Open():

The open() operation is used to open the file object. It accepts two arguments the name of the file and the mode in which the file will be opened.

file_object = open(file_name, mode)
  • Read():

The read() operation is used to read the contents of a file object.

file_object.read()
  • Write():

The write() operation is used to write to a file.

file_object.write("contents goes in here......")
  • Close():

The close() is used to close the file object after usage. A ValueErrorwill be raised for any more operations performed on the file object after closing it. Using the close() on opened files after usage is considered to be a good practice.

txt_file.close()

The mode for text file operations are as follows:

  • 'r' (read): open a file to read its content. An error will be thrown if the file is not found.

txt_file = open('technical.txt', 'r')

for content in txt_file:
    print (content)
txt_file.close()
  • The file content of technical.txt gets printed

There are several other ways to read the content of the file

txt_file = open('technical.txt', 'r')
#using the read()
print(txt_file.read())
#the read method accept an argument which 
#indicates the number of characters to read
print(txt_file.read(10))

#using with keyword
with open("technical.txt") as content:  
    file_content = content.read() 
print(file_content)
txt_file.close()
  • 'w' (write): open a file for a write operation. The w mode can create the file and write content to it if the file does not exist but it'll overwrite the content if it does exist.
txt_file = open('technical.txt','w')
#the \n is used to insert a new line after the content.
txt_file.write("I'm updating the content\n")
txt_file.write("I own a Youtube channel\n")
txt_file.write("The name of the Youtube channel is TechWithAlemsbaja\n")
txt_file.close()

The above code is used to write data into the technical.txt file. Let's retrieve the contents and see if the write was successful.

txt_file = open('technical.txt', 'r')
print(txt_file.read())

  • 'a' (append): open a file to append contents without overridden the existing content. In the code below we're appending some content that were not added initially to the technical.txt file.
txt_file = open('technical.txt','a')
txt_file.write("Forgot to share the link: https://www.youtube.com/channel/UCcbyqNvJwiKO0MbhhuXDjtw")
txt_file.close()

  • 'r+' (read or write): to perform read and write operation on a file. The existing content will be overridden.

  • 'w+': write and read data into the file. The existing content will be overridden.

  • 'a+' (append or read): append or read data without overridden the existing content in file.

  • x (exclusive creation): Enables writing into the file if it does not exist but it throws an error if the file exist.

      txt_file = open('technical.txt', 'x')
    
      #FileExistsError: [Errno 17] File exists: 'technical.txt'
    

Modes for Binary files

  • 'wb' (write to binary file) :
binary_file = open('technical.txt', 'wb')
  • 'rb' (read from binary file): opens the file in binary mode instead of text mode.
binary_file = open('technical.txt', 'rb')
  • 'ab' (append to binary file): append data to the opened text file in binary format.
binary_file = open('technical.txt', 'ab')
  • 'rb+' (read from a binary file and override existing content)
binary_file = open('technical.txt', 'rb+')
  • 'ab+' (append to a file without overriding the existing content)
binary_file = open('technical.txt', 'ab+')

Rename File in Python

The rename() from the os module method is used to change the name of a file. The first argument is the current name of the file and the second argument is the new name of the file.

import os 
os.rename('technical.txt', 'technical_new.txt')

Delete File in Python

The remove() from the os module method is used to delete the file.

💡
Caution is needed when using the remove() to avoid deleting important files.
import os 
os.remove("technical_new.txt")

#deletes the technical_new.txt file

File encoding format

The .encoding object is used to retrieve the encoded format value of the file.

txt_file = open('technical.txt', 'r')
print(txt_file.encoding)
print(txt_file.read())
txt_file.close()

#cp1252 file encoding

Useful Attributes / Functions of File Handling in Python

  • fileno(): returns the integer number of the file
print(txt_file.fileno())
#3
  • read(n): reads n number of characters from a file.
print(txt_file.read(10))
  • readable(): return true if a file is readable
print(txt_file.readable())
#True
  • readline(): read and return a single line from the file.
print(txt_file.readline())

#I'm updating the content
  • readlines(): read and returns all the lines in the file.
print(txt_file.readlines())
#["I'm updating the content\n", 'I own a Youtube channel\n', 'The name of the Youtube channel is TechWithAlemsbaja\n']
  • seek(offset): alter the cursor position by the number of bytes indicated by the offset. The example below moves the cursor to the character "u" from "I" which marks the beginning of the output.
txt_file.seek(4)
print(txt_file.readlines())

#updating the content
#I own a Youtube channel
#The name of the Youtube channel is TechWithAlemsbaja
  • seekable(): if the file allows for random access, it returns True.
print(txt_file.seekable())
#True
  • tell(): display the current cursor location value of the content in the file.
txt_file.seek(8)
print(txt_file.tell())
print(txt_file.read())

#8
  • writable(): it returns True if the file is writable
print(txt_file.writable())
#False
  • write(): to write string of data to the opened file as shown in the previous example.
txt_file = open('technical.txt','w')
#the \n is used to insert a new line after the content.
txt_file.write("I'm updating the content\n")
txt_file.write("I own a Youtube channel\n")
txt_file.write("The name of the Youtube channel is TechWithAlemsbaja\n")
txt_file.close()
  • writelines(sequence): to write multiple lines of content (data) to the opened file. It accepts an iterable object (list, tuple) as an argument.
txt_file = open('technical.txt','w+')
#the \n is used to insert a new line after the content.
txt_file.writelines(["I'm updating the content\n", "Something like this\n", "Just trying hands on key stuff"])
txt_file.close()
  • truncate(size=None): The file is truncated to a maximum of size bytes. If size is left empty, the file will be truncated in its entirety.
txt_file.truncate(4)
#truncate
  • detach(): the .detach() is called and divides the underlying raw stream from the buffer if the file exists.
txt_file = open('technical.txt', 'r')
if txt_file:
    print('Technical Text file opened!')
print(txt_file.detach())

#Technical Text file opened!
#<_io.BufferedReader name='technical.txt'>
  • isatty(): it is used to check if the file is connected to a terminal.
txt_file = open('technical.txt', 'r')
print(txt_file.isatty())
print(txt_file.read())
txt_file.close()

#False
  • flush(): it is used to clean out the internal buffer. When writing to a file that is being accessed by several threads or processes, this is helpful. It takes no arguments and does not return any response.
txt_file = open('technical.txt','w+')
#the \n is used to insert a new line after the content.
txt_file.writelines(["I'm updating the content\n", "Something like this\n", "Just trying hands on key stuff\n"])
txt_file.close()

txt_file = open('technical.txt','r')
print(f'File content before flush operation\n {txt_file.read()}')

txt_file.flush()
print(f'File content after flush operation {txt_file.read()}')

txt_file.close()

  • Example 2
txt_file = open('technical.txt','r')
txt_file.flush()
print(txt_file.readlines())
txt_file.close()

Copying files in Python

The shutil is a module in Python that is used to perform copy operation.

import shutil
import os 

shutil.copy2('technical.txt', 'technical_copy.txt')
# OR
shutil.copyfile('technical.txt', 'technical_copy.txt')

# the getcwd() is used to get the current working directory
current_dir = os.getcwd() 
# the listdir() is used to get the current working directory
directories = os.listdir(current_dir)
print(f'Found {len(directories)} directories in {current_dir}')
print('Next Line') 
for directory in directories: 
    print(directory)

In the example above we displayed two methods copy2 and copyfile for copying the contents of a file to another file (creates one if the file does not exist) passed as the second argument.

Using the os module to display the files in the current directory after the copy operation. Will explain directories in Python in another article.

Try-Except Blocks in Python File Handling

Looking at the sensitive of file operations it is essential to leverage on the try catch block to handle program execution if an exception occurs.

try:
    txt_file = open("technical_item.txt")
except IOError:
    print(f"Error: could not read file technical_item.txt")
#Error: could not read file technical_item.txt

If any error is encountered it basically prints the formatted response.

💡
That's a lot right? Let's wrap up with this: Practice! Practice!! Practice!!! is the best way to familiarize with file handling in Python programming.

Conclusion

There are several levels in application development but at some point there are there will be need to perform operation (read, write, open, close) on files. In this article we have dealt with file handling in Python and its attribute extensively with practical's. It is handy to prevent loss of data when the application is running because it uses the RAM (volatile memory). File handling in Python is useful for different kinds of operation from building interfaces that can manage file operations, cross platform support, to been able to read, modify and delete files or folders. However, there's need for security implementation and caution to avoid allowing users to easily manipulate the operating system directories and files.

Find this helpful or resourceful?? kindly share and feel free to use the comment section for questions, answers, and contributions.

💡
Follow me on Hashnode: Alemsbaja X: Alemsbaja | Youtube: Tech with Alemsbaja to stay updated on more articles

Did you find this article valuable?

Support Alemoh Rapheal Baja by becoming a sponsor. Any amount is appreciated!