Introduction to file handling of python

In this tutorial, you are going to learn about the file handling of python. Using file handling, we can store data permanently in your hard disk. There are two types of file:

  1. Text file: It contains the human-readable characters.
  2. Binary file: It contains the executable instruction.

Modes of a file in Python:

Generally, in python there are a total of 8 modes of file:-

  1. r : It will read the file only when the file exists.
  2. r+ : It will read and write the file only when the file exists.
  3. w : This mode will write the file in both the condition, i.e., the file exists or the file not exist. If the file exist then it will write to that file. In the non exists the case of the file, it will create the one and then write in it.
  4. w+ : This mode will read and write the file in both the condition of file exist or not same as the w mode.
  5. a : It will write the file in append mode when the file exists or it does not exist. If the file exists, then it will append in that file at the end of the file. In the non exist case of the file, it will create the one and then write in it.
  6. a+: It will read and write the file in append mode in both the condition of file exist or not the same as the a mode.
  7. x: This mode will write the file only when the file will not exist as it will create the one and write in it.
  8. x+: This mode will read and write the file in the same condition as the x mode.

Note: The default type of mode is t (i.e., text file) which means that the mode r, r+, w, …etc will read the text file only. The binary file can be read by adding b in the mode. For example: rb, rb+, wb, wb+, ….etc.

Steps used in Python for file handling in Python

  1. Open the file in a specific mode.
    file=open(file path, mode and file type)
  2. Perform read or write operations.
    file.read() or file.write()
  3. Close the file.
    file.close()

Different operations in file handling in Python

  1. How to create or open a file in Python
    The mode w will create the text file ‘demo’ if the file will not exist. Otherwise, if the file exists,  then it will open the ‘demo’ file.

    # it will create the text file
    oj=open('demo.txt','w')
    print("Created the file.")
    oj.close()
    

    Output:-

    Created the file.

    Note: The demo file will be saved in that location where your program has been saved. For example, My programs will save to the desktop. So, my demo file will be created and saved on the desktop.

  2. How to read data, write data and close the file in Python
    Open the file demo in w+ mode. Write the file demo and the pointer goes at the end of the line after the completion of write operation. Get the pointer at the zeroth position by using seek() method. Read the data from the file. Close the file.




    # Open the file
    oj2=open('demo.txt','w+')
    # Write the file
    oj2.write(" This is the text file.")
    # to get the pointer at the zeroth position
    oj2.seek(0)
    # Read the file
    print(oj2.read())
    # Close the file
    oj2.close()
    

    Output:-

    This is the text file.
  3. To create a directory in Python
    The mkdir() method of the os module will create a directory in the current directory. You need to supply an argument to this method which contains the name of the directory to be created.

    import os
    # this will create the folder
    os.mkdir("Python")
    
  4. To remove directory in Python
    rmdir() will remove the directory. Pass the name of the directory which you want to remove as the argument of the rmdir().
    Note: rmdir() will remove the directory only when it is empty ( it doesn’t contain any file).

    import os
    # to remove the directory
    os.rmdir("Python")
  5. How to rename a file in Python
    rename() will change the name of the existing file.
    Syntax: os.rename(existing_file_name, new_file_name)

    import os
    os.rename("demo.txt","blog.txt")
  6. How to walk in folder, sub-folder and files in Python
    os.walk() will walk through your current directory and return the path to the folder, subfolders and files.

    import os
    for folder, subfolder, files in os.walk(os.getcwd()):
        print(folder)
        print(subfolder)
        print(files)
    
  7. How to move a file
    How to move a file from one directory to another in Python
  8. To delete a file
    How to delete a file in Python with examples


Leave a Reply

Your email address will not be published. Required fields are marked *