Python Files Handling Tutorial

Hello Guys, we know that there is some situation where we need to work with Files instead of working with Databases.So in this blog, I’ll cover some important topic of Python Files Handling.

Guys, it’s very easy to work with Files using Python Files Handling. We can store useful information in a file using Python Files Handling.So without wasting time on talking let gets started with code

Python Files Handling

First for working with files, we have to open it.So the command for opening it is

  • f=open(“name of file”,”mode of file”)
  • It takes two argument
  • 1.Name of file to open
  • 2.Mode of file [Reading, Writing,Appending]
  • Where ‘ f ‘ is the File Handler or object , it uses to perform many operations with file method that we will see in a few minutes
  • Following are the modes of file that we use while opening a file
  • Modes Function
    r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the
    default mode
    rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the
    file. This is the default mode.
    r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
    rb+ Opens a file for both reading and writing in binary format. The file pointer will be at the
    beginning of the file.
    w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist,
    creates a new file for writing.
    wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does
    not exist, creates a new file for writing.
    w Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
    does not exist, creates a new file for reading and writing.
    w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
    does not exist, creates a new file for reading and writing
    wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
    exists. If the file does not exist, creates a new file for reading and writing.
    a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the
    file is in the append mode. If the file does not exist, it creates a new file for writing.
    ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file
    exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
    writing.
    a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file
    exists. The file opens in the append mode. If the file does not exist, it creates a new file for
    reading and writing.
    ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of
    the file if the file exists. The file opens in the append mode. If the file does not exist, it creates
    a new file for reading and writing.
  • Let’s create a Python File in your Desktop and write the code as given in image to perform Read and Write Operation
  • Python Files
  • Now see in your Desktop,there is a file named as “file1.txt”
    is created, just open it and match with the write statement
  • Python Files
  • Now its time to Read Data from a file.Look at the code given in image to Read Data from file1.txt
  • After executing this file you will get the output of 10 bytes of data from the file1.txt
  • Python Files
  • But we if we want to read whole data, then we have to use readlines() function.It read all the lines until it reaches EOF and returns a list containing the lines.see the code in image
  • Python Files
  • You will get the output as given below
  • Python Files
  • Following are the some Methods that can be used with File operation
  • Method Description
    tell() tell the current position within the file
    seek(offset, from) changes the current file position.The offset argument indicates the number of bytes to be moved. The from argument specifies the reference position from where the bytes are to be moved.[ex- f.seek(5,0)]
    truncate() truncates the file’s size. If the optional size argument is present, the file is truncated to (at most) that size.
    writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value
    rename() This method change the file name.It use with os module,It takes two arguments, the current filename and the new filename [import os,then os.rename(current_fname,new_fname]
    remove() This method delete the file,just you have to pass the name of file you want to delete.it also require to import os module to use it [import os,then os.remove(file_name]

For more details you can visit python official website https://docs.python.org/2/tutorial/inputoutput.html

Hope guys you will understand the concept of Python Files Handling clearly.Try to implement it with more code,and if you find any difficulty, feel free to drop a comment on this post.