File Management in python

PYTHON FILE HANDLING

  • Till now, we were taking the input from the console and writing it back to the console to interact with the user.
  • Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console since the memory is volatile, it is impossible to recover the programmatically generated data again and again.
  • The file handling plays an important role when the data needs to be stored permanently into the file. A file is a named location on disk to store related information. We can access the stored information (non-volatile) after the program termination.
  • The file-handling implementation is slightly lengthy or complicated in the other programming language, but it is easier and shorter in Python.
  • In Python, files are treated in two modes as text or binary. The file may be in the text or binary format, and each line of a file is ended with the special character.
  • Hence, a file operation can be done in the following order.
    • Open a file
    • Read or write - Performing operation
    • Close the file

OPENING A FILE

  • Python provides an open() function that accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc.
  • Syntax:

  • The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.

  • Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read mode and printing its content on the console.
  • Example

  • Output:

  • In the above code, we have passed filename as a first argument and opened file in read mode as we mentioned r as the second argument. The fileptr holds the file object and if the file is opened successfully, it will execute the print statement

THE CLOSE() METHOD

  • Once all the operations are done on the file, we must close it through our Python script using the close() method. Any unwritten information gets destroyed once the close() method is called on a file object.
  • We can perform any operation on the file externally using the file system which is the currently opened in Python; hence it is good practice to close the file once all the operations are done.
  • The syntax to use the close() method is given below.
  • Syntax

  • Consider the following example.

  • After closing the file, we cannot perform any operation in the file. The file needs to be properly closed. If any exception occurs while performing some operations in the file then the program terminates without closing the file.
  • We should use the following method to overcome such type of problem.

THE WITH STATEMENT

  • The with statement was introduced in python 2.5. The with statement is useful in the case of manipulating the files. It is used in the scenario where a pair of statements is to be executed with a block of code in between.
  • The syntax to open a file using with the statement is given below.

  • The advantage of using with statement is that it provides the guarantee to close the file regardless of how the nested block exits.
  • It is always suggestible to use the with statement in the case of files because, if the break, return, or exception occurs in the nested block of code then it automatically closes the file, we don't need to write the close() function. It doesn't let the file to corrupt.
  • Consider the following example.

WRITING THE FILE

  • To write some text to a file, we need to open the file using the open method with one of the following access modes.
    • w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
    • a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file exists.
  • Consider the following example.
  • Output: File2.txt
  • We have opened the file in w mode. The file1.txt file doesn't exist, it created a new file and we have written the content in the file using the write() function.
  • Example
  • Output
  • We can see that the content of the file is modified. We have opened the file in a mode and it appended the content in the existing file2.txt.

READ THE FILE

  • To read a file using the Python script, the Python provides the read() method. The read() method reads a string from the file. It can read the data in the text as well as a binary format.
  • The syntax of the read() method is given below.
  • Syntax:
  • Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the count is not specified, then it may read the content of the file until the end.
  • Consider the following example.
  • Output
  • In the above code, we have read the content of file2.txt by using the read() function. We have passed count value as ten which means it will read the first ten characters from the file.
  • If we use the following line, then it will print all content of the file.
  • Output

READ FILE THROUGH FOR LOOP

  • We can read the file using for loop. Consider the following example.

READ LINES OF THE FILE

  • Python facilitates to read the file line by line by using a function readline() method. The readline() method reads the lines of the file from the beginning, i.e., if we use the readline() method two times, then we can get the first two lines of the file.
  • Consider the following example which contains a function readline() that reads the first line of our file "file2.txt" containing three lines. Consider the following example.
  • Example Reading lines using readline() function
  • Output
  • We called the readline() function two times that's why it read two lines from the file.
  • Python provides also the readlines() method which is used for the reading lines. It returns the list of the lines till the end of file(EOF) is reached.
  • Example Reading Lines Using readlines() function
  • Output

CREATING A NEW FILE

  • The new file can be created by using one of the following access modes with the function open().
    • x: it creates a new file with the specified name. It causes an error a file exists with the same name.
    • a: It creates a new file with the specified name if no such file exists. It appends the content to the file if the file already exists with the specified name.
    • w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.
  • Consider the following example.
  • Output

FILE POINTER POSITIONS

  • Python provides the tell() method which is used to print the byte number at which the file pointer currently exists. Consider the following example.
  • Output

MODIFYING FILE POINTER POSITION

  • In real-world applications, sometimes we need to change the file pointer location externally since we may need to read or write the content at various locations.
  • For this purpose, the Python provides us the seek() method which enables us to modify the file pointer position externally.
  • The syntax to use the seek() method is given below.
  • Syntax:
  • The seek() method accepts two parameters:
    • offset: It refers to the new position of the file pointer within the file.
    • from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the beginning of the file is used as the reference position. If it is set to 1, the current position of the file pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the reference position.
  • Consider the following example.
  • Output

PYTHON OS MODULE

Renaming the file

  • The Python os module enables interaction with the operating system. The os module provides the functions that are involved in file processing operations like renaming, deleting, etc. It provides us the rename() method to rename the specified file to a new name. The syntax to use the rename() method is given below.
  • Syntax:
  • The first argument is the current file name and the second argument is the modified name. We can change the file name bypassing these two arguments. rename(current-name, new-name)
  • Example

Removing the file

  • The os module provides the remove() method which is used to remove the specified file. The syntax to use the remove() method is given below.
  • Example

CREATING THE NEW DIRECTORY

  • The mkdir() method is used to create the directories in the current working directory. The syntax to create the new directory is given below.
  • Syntax:
  • Example

THE getcwd() METHOD

  • This method returns the current working directory.
  • The syntax to use the getcwd() method is given below.
  • Syntax
  • Example
  • Output:

CHANGING THE CURRENT WORKING DIRECTORY

  • The chdir() method is used to change the current working directory to a specified directory.
  • The syntax to use the chdir() method is given below.
  • Syntax
  • Example
  • Output:

DELETING DIRECTORY

  • The rmdir() method is used to delete the specified directory.
  • The syntax to use the rmdir() method is given below.
  • Syntax
  • Example
  • It will remove the specified directory.

WRITING PYTHON OUTPUT TO THE FILES

  • In Python, there are the requirements to write the output of a Python script to a file.
  • The check_call() method of module subprocess is used to execute a Python script and write the output of that script to a file.
  • The following example contains two python scripts. The script file1.py executes the script file.py and writes its output to the text file output.txt.
  • Example: file1.py
  • file.py

THE FILE RELATED METHODS

  • The file object provides the following methods to manipulate the files on various operating systems.

Comments

Popular posts from this blog

Modules

Control Structures

Classes and Objects