This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

1. File modification function:

Write a file modification function. When calling the function, pass in three parameters (modified file path, to be modified, modified content) to complete the modification of the fileCopy the code
def alter_file(path,src_data,update_data): with open(path,mode='rt',encoding='utf-8') as f: res = f.read() res = res.replace(src_data,update_data) with open(path,mode='wt',encoding='utf-8') as f: F.write (res) print(' edit done!! ')Copy the code

Source file content:

1.txt

Soon after Yao's jianyi reached its peak, the journey also came to the desert in the clouds. Few people know that Yao, who lives in Chang 'an, was born under a mirage in the middle of the clouds. He returns with the glory of the sword fairy, only to find that the exotic city of his childhood is gone. The princess, who had been sold into slavery, told him her story: The empire's iron riders had crossed the Great Wall and leveled the desert. The deserted city was soon buried in sand. Yao wants to save her, but the girl chose to take his own life from humiliation.Copy the code

1. First, we need to set parameter variables, namely path, src_data,update_data, we first write these three variables dead, and finally encapsulated as a function

Path = r 'e :\Download\1.txt' src_data = ' 'update_data =' 'Copy the code

2. Open the file and read out the contents of the file. Then replace src_dat with update_data

 with open(path,mode='rt',encoding='utf-8') as f:
     res = f.read()
     res = res.replace(src_data,update_data)
Copy the code

3. Finally, open the file and write it

With open(path,mode='wt',encoding=' utF-8 ') as f: f.rite (res) print(' ')Copy the code

4. Finally encapsulate into a function, change variables into parameters

def alter_file(path,src_data,update_data): with open(path,mode='rt',encoding='utf-8') as f: res = f.read() res = res.replace(src_data,update_data) with open(path,mode='wt',encoding='utf-8') as f: F.write (res) print(' edit done!! ')Copy the code

2. Write the tail tool

Def tail_tool(path): import time with open(path,mode='rb') as f: f.aim (0,2) while True: If len(res) == 0: time.sleep(0.5) else: res = res.decode(' utF-8 ') print(res)Copy the code

1. This is to monitor the end of a file in real time. Let’s first set the parameter variable, here is the path of the file, and then import the time module

 path = r'E:\Download\1.txt'
 import time
Copy the code

2. Then open it by reading binary and place the pointer on the last line

With open(path,mode='rb') as f: f.sek (0,2)Copy the code

3. Then start real-time monitoring, that is, write an infinite loop, each time read the last line, if the length of the last line is 0, it is not added to the content, take a break, and then continue to read, if the length is not 0, it is added to the content, the content is encoded into UTF-8 and printed out

While True: res = f.readline() if len(res) == 0: time.sleep(0.5) else: res = res.decode(' utF-8 ')Copy the code

4. Wrap it as a function

Def tail_tool(path): import time with open(path,mode='rb') as f: f.aim (0,2) while True: If len(res) == 0: time.sleep(0.5) else: res = res.decode(' utF-8 ') print(res)Copy the code

\