Tuesday, November 27, 2018

More on File Handling in Python

Opening and Reading Text Files by Buffer Size

The read() function in Python allows us to specify the buffer size we want so that our program does not use too much memory resources. The read() takes a parameter which specifies the data to be fetched from a file in bytes. Lets see this through a program:

sourcefile = open('cities.txt','r')
targetfile = open('mycities.txt','w')

mytext = sourcefile.read(10)

while len(mytext):
   
    targetfile.write(mytext)
    mytext = sourcefile.read(10)



We started by opening our cities.txt file in read mode with  sourcefile as file handler/file object. Next we created a new file mycities.txt and opened it in write mode with targetfile as file handler. In the line mytext = sourcefile.read(10), the read function as an argument , 10, which tells the read() to only read 10 bytes. The content from cities.txt is saved in the variable mytext.

In the while statement we have use this mytext variable to check when its length is zero. As long as the length is not zero, the loop will run and content from mytext will be written to mycities.txt file. After writing the message again the next 10 bytes data/content is read from cities.txt and saved in the variable mytext. This goes on until the entire content from cities.txt is saved in the mycities.txt file.

If mycities.txt didn't exist then it'll be created when we use open('mycities.txt','w'). The output of mycities.txt file should be the same as that of cities.txt file as shown below:

Hyderabad
Mumbai
Delhi
Chennai
Kolkata
Bengaluru
Jabalpur

How to verify that only 10 bytes of content was read at a time? try the program below and see the output:

sourcefile = open('cities.txt','r')
targetfile = open('mycities.txt','w')

mytext = sourcefile.read(10)

while len(mytext):
   
    targetfile.write(mytext + '\n')
    mytext = sourcefile.read(10)

   

The output of mycities.txt file should now be:
    
Hyderabad

Mumbai
Del
hi
Chennai

Kolkata
B
engaluru
J
abalpur


Handling binary files

The read and write operations of Binary files are also handled like text files, the only difference is in the syntax used in opening the file. We use 'rb', 'wb' instead of 'r' and 'w' for the opening modes. The program shown below explains the read and write operations.

sourcefile = open('m2.jpg','rb')
targetfile = open('mym2.jpg','wb')

myimage = sourcefile.read()
targetfile.write(myimage)



When we run the program an additional image file named mym2.jpg is created in our examples folder where m2.jpg is present. It looks exactly like m2.jpg

Deleting and Renaming Files

In Python the two functions used deleting and renaming files are the remove() and rename() functions. These functions are available in the os module and have to be imported before we can use them.

1. remove() function

The syntax to remove file is remove("filename"). To remove/delete mycities.txt file we will use the following code:

import os

os.remove("mycities.txt")



When we run the program the file mycities.txt should be deleted from the stored location.

2. rename() function    
   
The syntax to rename file is rename("oldfilename","newfilename"). To rename cities.txt file to newcities.txt  we will use the following code:

import os

os.rename("cities.txt","newcities.txt")


When we run the program the file cities.txt should be renamed to newcities.txt in the stored location.


Creating New Folders

Through programs we can create new folders (directories) with the os.makedirs() function. In order to create a new folder 'MyCode' in F:\Python_Code\examples we'll use the following code:

import os

os.makedirs('F:\Python_Code\examples\MyCode')


This creates a new folder MyCode inside the examples directory

If we used 'C:\Python_Code\examples\MyCode' with makedirs(), a new directory structure will be created from scratch in the C: drive .

After running the code just check if the desired folder and directory structure was created at the expected locations.

There is a separate os.path module which contains many helpful functions related to filenames
and file paths. In the next post we shall focus on this module. Till we meet next keep practicing and learning Python as Python is....






Share:

0 comments:

Post a Comment