Append Files in PythonAn Overview of File Handling in PythonHandling files in Python helps the programmers read and write files. Python provides various functions to handle files. Generally, there are two types of files: Text files and Binary files. Basic Terminologies and Important Concepts of File Handling
We will understand different functions and methods to append the file in Python. Opening a file in append mode shifts the pointer to the end of the file and inserts the data just after the existing data. Let's understand the concept of append files using some examples in Python. The given text file is the input file to which we will append the data. Program 1: Open an Already Existing File in Append ModeOutput: hello, this is a demo file.OK We have created a text file (demo.txt) and added some content. We created an object to handle the file. Using the open( ) function, we opened an existing text file in append mode ('a'). Then, we have added some data using the write( ) function. Then, we closed the file using the close( ) function. Again, we opened the file in read mode and printed the file's content using the read( ) function. It will print the data just after the existing data of the file. The append mode will override the data, and the changes will also be visible in the original file. Program 2: Appends Data in the File in a New LineWhen we append a file, it adds the data just after the last data in the file but not in the next line. We can append the data in the next line by adding \n in the file.write( ) function. We can open the file with append or write mode. It will move the pointer to the end of the file. We will append the newline character (\n), adding the data to the new line. Output: hello, this is a demo file. OK Adding a new Line Program 3: Append the File using a+ Mode.The Append and Read mode (a+) is used to open the file, append it, and then read it. Output: hello, this is a demo file.OK Adding a new Line This is append mode We opened the text file (demo.txt) in a+ mode, by which the pointer moves to the end of the file. The write( ) function was used to add the data to the file and then printed it using the read( ) function. There is no need to open a file two times in append and read mode. Both read and write functions can be used in append and read mode (a+). Program 4: Using with Statement to Append the FileThe file can also be opened using the with statement. Using with statement, we don't need to close the file using the close( ) function. It automatically closes the file after its execution. Output: hello, this is a demo file.OK Adding a new Line This is append mode Append the file using with statement We can also execute this program without closing the file: Program 5: Adding Multiple Lines in the FileOutput: hello, this is a demo file.OK Adding a new Line This is append mode Append the file using with statement New Line Adding Multiple Lines in the file We have opened the text file (demo.txt) in a+ mode (append and write mode), declared a variable 'lines', and defined a list of content we want to append. Then, using the writelines( ) function, we write the content. We can pass any list, tuple, or iterable to append the file. We can add multiple lines in just one line by defining a list or tuple. Program 6: Create a New File and Append it.We can create a new file using the write mode and can append the file using the append mode. Output: Creating a new file In write mode... Adding data in a new file In append mode... We created a new text file (text.txt) by opening the file in write mode. Then, added the data using the writelines( ) function. We closed the file using the close( ) function. Now, we opened the file using append mode, which will take the pointer at the end of the file and appended the file using the writelines( ) function. Next TopicBest-fit-algorithm-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India