Python3 File Handling

Python  File Handling

FILE  I/O

         File is a named location on disk to store related information. It is used to store data permanently in a non-volatile memory (eg. hard disk).

 Since, random access memory (RAM) is volatile which loses its data when computer is turned off ,we use files for the future use of the data
 
 When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.

 File operation :

  1.  Open a file
  2.  Read or write(perform operation)
  3.  Close the file

Opening a File

       Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.

       #open file in current directory
  
        f=open('example.txt')

         we can specify the mode while opening a file. In mode, we specify whether we want to read 'r' , write 'w' or append 'a' to the file. We also specify if we want to open the file in text mode or binary mode.

Python File Modes :

   'r' -  open a file for reading.(default)
  'w' - open a file for writing. Create a new file if it does not exist or truncates the file if it exists
  'x' - open a file for exclusive creation. if the file already exists, the operation fails.
  'a' - open for appending at the end of the file without truncating it. Create a new file if it does not          exist.
  't' - open in text mode. (default)
  'b' - open in binary mode.
  '+' - open a file for updating (reading and writing)

       f=open('example.txt')       #equivalent to 'r'
       f=open('example.txt','r')

       f=open('test.txt','w')

 The default encoding is platform dependent. In windows. it is 'cp1252' but 'utf-8' in Linux.

 So, we must not also rely on the default encoding or else our code will behave differently in different platforms.

 Hence, when working with files in text mode, it is highly recommended to specify the encoding type.

Closing a File

 Closing a file will free up the resources that were tied with the file and is done using the close() method.

 Python has a garbage collector to clean up unreferenced object but, we must not rely on it to close the file.

       f=open('example.txt')
       f.close()

 This method is not entirely safe. If an exception occurs when we are performing some operation with the file, the code exits without closing the file.

 A safer way is to use a try....finally block.    

       try:
           f=open("example.txt")
           # perform file operations

       finally:
            f.close()

 This way, we are guaranteed that the file is properly closed even if an exception is raised, causing program flow to stop

The best way to do this is using the with statement. This ensures that the file is closed when the block inside with is exited.

 We don't need to explicitly call the close() method. it is done internally.
 
 with open("example.txt",encoding='utf-8') as f:

         #perform file operations 

 


Writing to a File

 In order to write into a file we need to open it in write 'w', append 'a' or exclusive creation 'x' mode.
 
 We need to be careful with the 'w' mode as it will overwrite into the file if it already exists. All previous data are erased.
 
Writing a string or sequence of bytes(for binary files) is done using write() method. This method returns the number of characters written to the file.

Eg:
       f=open("text.txt", 'w')
       f.write("this is a first file\n")
       f.write("contains two lines\n")
       f.close()


output: This program will create a new file named 'test.txt' if it does not exist. if it does exists, it is overwritten.

Reading From a File

  There are various method available for this purpose. We can use the read(size) method to read in size number of data. If size parameter is not specified, it reads and returns up to the end of the file.

       f=open("text.txt","r")
       f.read()

output:  'this is a first file\n contains two lines\n'

       f=open("text.txt","r")
       f.read(4)

output:  'this'

        #f=open("text.txt","r")
       f.read(10)

output:  '  is a firs'

   * We can change our current file cursor(position) using the seek() method.

   *  Similarly, the tell() method returns our current position (in number of bytes).

       f.tell()

output:  14
 
       f.seek(0)     #bring the file cursor to initial position

output:  0

       print(f.read())         #read the entire file

output:  this is a first file
               contains two lines


We can read a file line-by-line using a for loop. This is both efficient and fast.

       f.seek(0)
       for line in f:
             print(line)

output:  this is a first file
            
              contains two lines

Alternately, we can use readline() method to read individual lines of a file This method reads a file till the newline, including the newline character.

       f=open("text.txt","r")
       f.readline()

output:  'this is a first file\n'
      
       f.readline()
         
output:   'contains two lines\n'

       f.readline()
 
output:   ' '

            The readlines() method returns a list of remaining lines of the entire file. All these reading method return empty values when end of file(EOF) is reached. 

       f.seek(0)
       f.readlines() 

output:   [ 'this is a first file\n', 'contains two lines\n' ]
 
 Renaming And Deleting Files In Python.

  While you were using read/write function, you may also need to rename/delete a file in Python. So, there comes a OS module in Python which bring the support of file rename/delete operations.

 So, to continue, first of all, you should import the OS module in your Python script.

       import os

       #rename a file from text.txt to sample.txt
       os.rename("text.txt',"sample.txt")

       f=open("sample.txt","r")
       f.readline()

output:   'this is a first file\n'

       #delete a file sample.txt
       os.remove("sample.txt")

       f=open("sample.txt","r")
       f.readline()

output:   ----------------------------------------------------------------------
                --
                FileNotFoundError

               FileNotFoundError :  no such file or directory : "sample.txt"

Python Directory and File Management :

 If there are a large number of files to handle in your python program ,you can arrange your code within different directory to make things more manageable.

A directory or folder is a collection of files and sub directories. Python has the OS module, which provides us with many useful methods to work with directories (and files as well).

Get current directory

We can get the present working directory using the getcwd() method.
  
This method returns the current working directory in the form of a string.

       import os
       os.getcwd()

output:  '/Users/varma/Google Drive/OnlineVideos/2/python-basics/File operation'

 Changing Directory

  We can change the current working directory using the chdir() method.

 The new path that we want to change to must be supplied as a string to this method. We can use both forward slash(/) or the backward slash(\) to separate path elements.

       os.chdir("/Users/varma/")
     
       os.getcwd()

output:   '/Users/varma'

  List Directory and files

  All files and sub directory inside a directory can be known using the listdir() method.
 
       os.listdir(os.getcwd())

output:   ['.bash_history',
               '.bash_sessions',   
               '.bashrc',
               '.cache',
               '.CFUserTextEncoding',
               '.config',
  '.DS_store',
  'ipynb_checkpoints',
  '.ipython',
  'jupyter',
  '.keras',
  '.lesshst',
  '.macports',
  '.matplotlib',
  '.octave_hist',
  '.octave_packages',
  '.oracle_jre_usage',
  '.plotly',
  '.profile',
  '.profile_history',
  '.python_history',
  '.Rhistory',
  '........some more directories

 Making New Directory

 We can make a new directory using the mkdir() method.

This method takes in the path of the new directory. If the full path is not specified, the new directory is created in the current working directory.

       os.mkdir('test')

 However, note that mkdir() method can only remove empty directories.

In order to remove a non-empty directory we can use the rmtree() method inside the shutil module.

       os.rmdir('test')

       import shutil

       os.mkdir('test')
       os.chdir('./test')              #  ./   -represents current directory
       f=open("testfile.txt",'w')     # within test directory, I created testfile
       f.write("Hello World")
       os.chdir("../")             # ../ represents a parent directory of a current directory
       os.rmdir('test')

output:   ---------------------------------------------------------------
               OSError

               OsError :   Directory not empty :  'test'

       #remove an non-empty directory
       shutil.rmtree('test')

       os.getcwd()

output:    '/Users/varma'








Comments

Popular posts from this blog

Python3

Python3 Function Arguments