Python insert file into file

Reading and Writing to text files in Python

Python provides inbuilt functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s).

  • Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
  • Binary files: In this type of file, there is no terminator for a line, and the data is stored after converting it into machine-understandable binary language.

In this article, we will be focusing on opening, closing, reading, and writing data in a text file.

File Access Modes

Access modes govern the type of operations possible in the opened file. It refers to how the file will be used once its opened. These modes also define the location of the File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file. There are 6 access modes in python.

  1. Read Only (‘r’) : Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exists, raises the I/O error. This is also the default mode in which a file is opened.
  2. Read and Write (‘r+’): Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist.
  3. Write Only (‘w’) : Open the file for writing. For the existing files, the data is truncated and over-written. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
  4. Write and Read (‘w+’) : Open the file for reading and writing. For an existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
  5. Append Only (‘a’): Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
  6. Append and Read (‘a+’) : Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.
Читайте также:  Html таблица полей ввода

How Files are Loaded into Primary Memory

There are two kinds of memory in a computer i.e. Primary and Secondary memory every file that you saved or anyone saved is on secondary memory cause any data in primary memory is deleted when the computer is powered off. So when you need to change any text file or just to work with them in python you need to load that file into primary memory. Python interacts with files loaded in primary memory or main memory through “file handlers” ( This is how your operating system gives access to python to interact with the file you opened by searching the file in its memory if found it returns a file handler and then you can work with the file ).

Opening a File

It is done using the open() function. No module is required to be imported for this function.

File_object = open(r"File_Name","Access_Mode")

The file should exist in the same directory as the python program file else, the full address of the file should be written in place of the filename. Note: The r is placed before the filename to prevent the characters in the filename string to be treated as special characters. For example, if there is \temp in the file address, then \t is treated as the tab character, and an error is raised of invalid address. The r makes the string raw, that is, it tells that the string is without any special characters. The r can be ignored if the file is in the same directory and the address is not being placed.

Источник

How to Modify a Text File in Python?

Be on the Right Side of Change

Summary: You can modify a text file in Python using one of the following methods:

  • Using The seek() Method
  • Using The fileinput Module
  • Using The splitlines() Method
  • Using the regex module and the split() and insert() methods

Overview

Problem: Given a text file; how to modify it in Python?

Scenario 1 : Insert a New Line in The File

Consider that you have the following text file that lists certain websites and you want to insert another website (string) in a new-line into the file.

You intend to insert, “Freelancer.com” in the list given in the above file without deleting the file.

Scenario 2: Insert a New Sentence in The Same Line

In the previous example, you wanted to insert a string in a new line, what if you want to insert a new string at a certain place in a paragraph irrespective of the lines. For example, you have a file with the following sentences as shown below:

Peter Piper picked a peck of pickled peppers.A peck of pickled peppers Peter Piper picked.Wheres the peck of pickled peppers Peter Piper picked.

You want to insert a new sentence (for example – If Peter Piper picked a peck of pickled peppers) after the second sentence. So, the desired output is:

Peter Piper picked a peck of pickled peppers.A peck of pickled peppers Peter Piper picked.If Peter Piper picked a peck of pickled peppers.Wheres the peck of pickled peppers Peter Piper picked.

So, how are you going to accomplish the above tasks? ?

Before we proceed further please note that:

You cannot insert a string into the middle of a file without re-writing it. You can append to a file or overwrite part of it using the seek() method which we will dive into in a while; but if you wish to add something at the beginning or the middle of the file, you will have to rewrite it. As simple as that! ? This is completely an operating system thing and has nothing to do with Python. This is the case in all languages.

Therefore, the best practice is:

  • read from the file,
  • make the necessary modifications,
  • write it out to a new file ( for example ‘my_file.txt.tmp’). This is far better than reading the entire file into the memory, especially if the file is large.
  • Once the temporary file is completed, rename it to the same name as the original file.

This is an effective and safe way to do it because if for any reason the file write crashes or aborts then you still have your untouched original file.

Now that we have a clear understanding of the problem and the right approach to make the modifications, let us dive into the solutions and find out how we can implement our concept in Python!

Solutions to Scenario 1

❒ Method 1: Using The seek() Method

⁕ Definition and Usage of the seek() Method

seek() is a Python file method that allows you set the current file position in a file stream. It also returns the new position.

Let’s use the seek() method to modify our file.

⁕ The Solution

Case 1: Appending to the middle of the file.

with open('test.txt', 'r+') as f: file = f.readlines() for line in file: if 'Upwork' in line: pos = line.index('Upwork') file.insert(pos + 1, 'Freelancer.com\n') f.seek(0) f.writelines(file) f.close()

Case 2: Prepending to the End of file

If you just want to append to the file then all you have to do is open up the file in append mode and insert the required string.

with open('test.txt', 'a') as f: f.write('\nFreelancer.com')

Case 3: Prepending to the Beginning of the File

with open('test.txt', 'r+') as f: file = f.readlines() file.insert(0,'Freelancer.com\n') f.seek(0) f.writelines(file)

❒ Method 2: Using The fileinput Module

Another workaround to modify a file is that you can use the fileinput module of the Python standard library that allows you to rewrite a file by setting the inplace keyword argument as inplace=true .

import fileinput f = fileinput.input('test.txt', inplace=true) for n, line in enumerate(f, start=1): if line.strip() == 'Finxter': print('Freelancer.com') print(line, end='')

❒ Method 3: Using The splitlines() Method

❖ splitlines() is method in Python which is used to split a string breaking at line boundaries. It returns a list of the lines, breaking at line boundaries.

with open('test.txt', 'r+') as infile: data = infile.read() # Read the contents of the file into memory. # Return a list of the lines, breaking at line boundaries. li = data.splitlines() index = li.index('Upwork')+1 li.insert(index,'Freelancer.com') infile.seek(0) for item in li: infile.writelines(item+'\n')

The Solution to Scenario 2

Now let us have a quick look at the solution to our second scenario wherein we will be inserting a new sentence, irrespective of the line number. A simple solution to this problem is to split the sentences with full-stop as the separator and also store the separator along with the texts. Then you can insert the additional sentence in the required space and finally write it to the file.

import re f = open("demo.txt", "r+") contents = f.read() text = re.split('([.])', contents) text.insert(4, 'If Peter Piper picked a peck of pickled peppers.') f.seek(0) f.writelines(text) f.close()
Peter Piper picked a peck of pickled peppers.A peck of pickled peppers Peter Piper picked.If Peter Piper picked a peck of pickled peppers.Wheres the peck of pickled peppers Peter Piper picked.

You might want to have a look at another scenario described in the following post:

How to Search and Replace a Line in a File in Python?

Conclusion

In this article we discussed how to modify a file in Python with the help of a couple of scenarios. We used the following ways to reach our final solution:

With that, we come to the end of this article and I hope you can modify files in Python with ease after reading this article! Please stay tuned and subscribe for more interesting articles and discussions.

If you want to edit a text file in Windows PowerShell, feel free to check out this detailed article on the Finxter blog.

I am a professional Python Blogger and Content creator. I have published numerous articles and created courses over a period of time. Presently I am working as a full-time freelancer and I have experience in domains like Python, AWS, DevOps, and Networking.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Оцените статью