Python File HandlingTill 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.
Opening a filePython 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. ExampleOutput: <class '_io.TextIOWrapper'> file is opened successfully 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() methodOnce 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 statementThe 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. ExampleWriting the fileTo 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. ExampleOutput: File2.txt Python is the modern-day language. It makes things so simple. It is the fastest growing programming language. Snapshot of the 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 2Output: Python is the modern day language. It makes things so simple. It is the fastest growing programing language Python has an easy syntax and user-friendly interaction. Snapshot of the file2.txt 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. 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. ExampleOutput: <class 'str'> Python is 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: Python is the modern-day language. It makes things so simple. It is the fastest-growing programing language Python has easy an syntax and user-friendly interaction. Read file through for loopWe can read the file using for loop. Consider the following example. Output: Python is the modern day language. It makes things so simple. Python has easy syntax and user-friendly interaction. Read Lines of the filePython 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 1: Reading lines using readline() functionOutput: Python is the modern day language. It makes things so simple. 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 2: Reading Lines Using readlines() functionOutput: ['Python is the modern day language./n', 'It makes things so simple./n', 'Python has easy syntax and user-friendly interaction.'] Creating a new fileThe 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. Example 1Output: <_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'> File created successfully File Pointer positionsPython provides the tell() method which is used to print the byte number at which the file pointer currently exists. Consider the following example. Output: The filepointer is at byte : 0 After reading, the filepointer is at: 117 Modifying file pointer positionIn 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. ExampleOutput: The filepointer is at byte : 0 After reading, the filepointer is at: 10 Python OS moduleRenaming the fileThe 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. Example 1: Output: The above code renamed current file2.txt to file3.txt Removing the fileThe 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 1 Creating the new directoryThe 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 1 The getcwd() methodThis method returns the current working directory. The syntax to use the getcwd() method is given below. Syntax Example Output: 'C://Users//DEVANSH SHARMA' Changing the current working directoryThe 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 ExampleOutput: 'C://Users//DEVANSH SHARMA//Documents' Deleting directoryThe rmdir() method is used to delete the specified directory. The syntax to use the rmdir() method is given below. Syntax Example 1 It will remove the specified directory. Writing Python output to the filesIn 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 file.py file.py The file related methodsThe file object provides the following methods to manipulate the files on various operating systems.
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263685.html