[Basic knowledge of python] 16. Basics and operations of file reading and writing

Preface

File reading and writing is the main function of Python code to call computer files. It can be used to read and write text records, audio clips, Excel documents, saved emails, and anything saved on the computer.

You may be wondering: Why should I open a file in Python? Wouldn't it be better if I just open that file and operate on that file?

Generally speaking, it is of course no problem to open the operation directly. But if you have a job that requires you to merge the information in 100 Word documents into one file, it would be troublesome to copy and paste them one by one, then you can use Python. Or, when you want to download thousands of pieces of data from the Internet, it is also very convenient to directly use Python to help you save the data into a file at once.

Otherwise, Python frees us from repetitive work~

[File reading and writing] is divided into two parts: [reading] and [writing]. Let’s first take a look at how [reading files] is implemented?

read file

Insert image description here

In fact, it’s really just three steps:

Insert image description here
Is it like that bad joke from a long time ago? "How many steps does it take to put an elephant into the refrigerator?" Three steps: open the refrigerator, put the elephant in, and close the refrigerator. Similarly, reading a file is also a three-step process: open - read - close.

Let me give you an example. You can follow me on your own computer. If the Python environment is not installed on your computer yet, you can install it first.

First, let's create a new test folder on the desktop, and then create a txt file named abc in the folder, and write anything in it. I wrote Zhou Zhiruo and Zhao Min.

I open this file with the editor Visual Studio Code, like this:

Insert image description here
Then, you can use VS Code to create a new Python file of open.py and place it in the test folder, where we will write the code.

How to write the code?

【Step 1-Open】

Open the file using the open() function. The syntax is this:

file1 = open('/Users/Ted/Desktop/test/abc.txt','r',encoding='utf-8') 

The file1 variable stores the read file data in order to perform the next operation on the file.

There are three parameters in the open() function, right:

'/Users/Ted/Desktop/test/abc.txt'
'r'
encoding='utf-8'

Let’s look at them one by one. The first parameter is the storage address of the file, which must be written clearly, otherwise the computer cannot find it. Note: My file address and yours are different.

To find your file address, just drag the file you want to open directly into the editor terminal window, the file address will be displayed, and then copy it.

However, there are two types of file addresses: relative paths and absolute paths. The address obtained by dragging it to the terminal is an absolute path. These two addresses, Mac and Windows computers are a bit different. Let me help you figure it out.

The absolute path is the most complete path, and the relative path refers to the path [relative to the current folder], which is the folder path where the py file you wrote is placed!

If the file you want to open is in the same folder as open.py, then just use a relative path. If you want to use files in other folders, you need to use an absolute path.

Let's look at the Mac computer first. Now my txt files and py files are placed in the test folder on the desktop.

Insert image description here
I dragged the txt file into the terminal window and got the absolute path to the file:

Insert image description here
Insert image description here
Then when I open it with the open() function, I can write it as:

open('/Users/Ted/Desktop/test/abc.txt')   #绝对路径
open('abc.txt')    #相对路径
#相对路径也可以写成open('./abc.txt')

In this case, you can write both absolute and relative paths.

If the current txt file is placed in a folder called word under the test folder, the absolute path and relative path will become:

open('/Users/Ted/Desktop/test/word/abc.txt'')
open('word/abc.txt')

Let's look at Windows again. In Windows systems, \ is often used to represent absolute paths, and / is used to represent relative paths, so when you drag a file into the terminal, the absolute path becomes:

C:\Users\Ted\Desktop\test\abc.txt

However, don't forget that \ is an escape character in Python, so conflicts often occur. In order to avoid pitfalls, the absolute path of Windows usually needs to be slightly processed and written in the following two formats;

open('C:\\Users\\Ted\\Desktop\\test\\abc.txt')
#将'\'替换成'\\'

open(r'C:\Users\Ted\Desktop\test\abc.txt')
#在路径前加上字母r

There is another tip to get the relative path of a file. Open the folder with VS Code, right-click on the file and select:

Insert image description here
Now, copy this line of code into your open.py file and replace the file address with your own.

file1 = open('/Users/Ted/Desktop/test/abc.txt','r',encoding='utf-8') 

alright. Finally finished talking about the first parameter file address, let’s look back at the other parameters of open():

file1 = open('/Users/Ted/Desktop/test/abc.txt','r',encoding='utf-8')

The second parameter indicates the mode when opening the file. Here is the string 'r', which means read, indicating that we opened the file in read mode.

You may be wondering, why do you have to decide whether to read or write when you open it, but not decide later? This is because the computer pays great attention to the confidentiality of data and must decide in what mode to open the file when opening it.

In addition to 'r', there are other modes such as 'w' (write), 'a' (append), etc., which we will cover later.

The third parameter encoding='utf-8' indicates the encoding of the returned data, usually utf-8 or gbk. Note that encoding is written here instead of encode.

The three steps of reading a file: Open - Read - Close, [Step 1 - Open], we have finished talking about it, now look at [Step 2 - Read].

【Step 2-Read】

After opening the file file1, you can use the read() function to perform reading operations. Please see the code:

file1 = open('/Users/Ted/Desktop/test/abc.txt', 'r',encoding='utf-8') 
filecontent = file1.read()   

The first line of code is what we wrote before. The file "abc.txt" was opened for reading.

The second line of code is reading the content of file1. The writing method is to add a . period after the variable file1, add a read(), and put the read content into the variable filecontent, so that we can get the content of the file.

So, now we want to see what data has been read, we can use the print() function to see it. Please complete the remaining code on your own computer. Please refer to the following code.

file1 = open('/Users/Ted/Desktop/test/abc.txt','r',encoding='utf-8') 
filecontent = file1.read()   
print(filecontent)

Then, [right-click] in the editor window and select [Run Python file in terminal]. At this time, the terminal displays:

Insert image description here
You will find that when the contents of the abc.txt file are printed, it will be read as string data.

[Step 3-Close]

To close the file, use the close() function.

file1 = open('/Users/Ted/Desktop/test/abc.txt','r',encoding='utf-8') 
filecontent = file1.read()            
print(filecontent)
file1.close()      

You have learned the first three lines of code. Line 4: Add a dot after the variable file1, and then add close(), which means closing the file. Be sure to remember that the following parentheses cannot be lost.

Why should we close the file? There are two reasons: 1. There is a limit to the number of files that a computer can open. If there are too many open() calls without close(), the file will no longer be open. 2. It can ensure that the written content has been saved in the file.

After the file is closed, the file can no longer be read or written. If you still need to read and write this file, you must open() the file again.

Let's summarize the three steps of reading a file: opening - reading - closing, and provide a summary picture.

Insert image description here
What needs special attention is the second and third steps, that is, the writing method of reading and closing.

After learning [Reading Files], then [Writing Files].

write file

Insert image description here
Hehe, writing a file is also a three-step process: open the file - write the file - close the file.

Insert image description here

【Step 1-Open】

Open the file in writing mode.

file1 = open('/Users/Ted/Desktop/test/abc.txt','w',encoding='utf-8') 

Line 1 of code: The file "abc.txt" is opened in writing mode.

There are still three parameters in open(), and everything else is the same, except that the second parameter needs to be changed to 'w', which means write, that is, opening the file in writing mode.

[Step 2-Write]

To write content to a file, use the write() function.

file1 = open('/Users/Ted/Desktop/test/abc.txt', 'w',encoding='utf-8') 
file1.write('张无忌\n')     
file1.write('宋青书\n')  

Lines 2-3 of code: The two strings "Zhang Wuji" and "Song Qingshu" are written into the "abc.txt" file. \n means start a new line.

Please follow the same instructions and remember to run the program. Then when you open the txt file to view the data:

Insert image description here
Eh? Where did Zhou Zhiruo and Zhao Min go in the original document?

Here's the thing, the 'w' write mode will violently clear the file for you and then write it to you again. If you just want to add something without completely overwriting the original file, you need to use the 'a' mode, which means append. You have learned that it means append.

If we had to do it all over again, we would write like this:

file1 = open('/Users/Ted/Desktop/test/abc.txt', 'a',encoding='utf-8') 
#以追加的方式打开文件abc.txt
file1.write('张无忌\n')     
#把字符串'张无忌'写入文件file1
file1.write('宋青书\n')     
#把字符串'宋青书'写入文件file1

In this case, the addition will be successful and will not be overwritten. You can try adding anything, run it and see.

Insert image description here

[Step 3-Close]

Still remember to close the file, use the close() function, look at the code:

file1 = open('/Users/Ted/Desktop/test/abc.txt','a',encoding='utf-8') 
file1.write('张无忌\n')     
file1.write('宋青书\n')     
file1.close()   

The 4th line of code is still a familiar recipe and a familiar taste. This completes [writing files].

However, there are two small tips: 1. The write() function also writes text files of string type. 2. In 'w' and 'a' modes, if the file you open does not exist, the open() function will automatically create one for you.

[It’s time to practice]

1. Please write the string 'Hard to Recite Sutra' in a file called 1.txt. 2. Then please read the contents of this 1.txt file and print it out.

Tip: Write first, then read. Writing a file is divided into three steps, and reading a file is also divided into three steps.

Reference Code:

f1 = open('./1.txt','a',encoding='utf-8') 
#以追加的方式打开一个文件,尽管并不存在这个文件,但这行代码已经创建了一个txt文件了
f1.write('难念的经')
#写入'难念的经'的字符串
f1.close()           
#关闭文件 

f2 = open('./1.txt','r',encoding='utf-8')
#以读的方式打开这个文件
content = f2.read()
#把读取到的内容放在变量content里面
print(content)
#打印变量content
f2.close()
#关闭文件

Note: The path can be replaced according to your actual path.

operation result:

难念的经

Let’s summarize the three-step method for writing files.

Insert image description here
Now the question comes, what should we do if the data we want to write is not text content, but audio and pictures?

Insert image description here
We can see that there is 'wb' mode in it, which means opening a file in binary mode for writing. Because pictures and audio are saved in binary form, it is good to use wb mode, which we will use in today's homework.

Tips

Here is another usage. In order to avoid forgetting to close the file after opening it, occupying resources, or when the appropriate time to close the file is not determined, we can use the keyword with. The previous example can be written like this:

# 普通写法
file1 = open('abc.txt','a') 
file1.write('张无忌') 
file1.close()

# 使用with关键字的写法
with open('abc.txt','a') as file1:
#with open('文件地址','读写模式') as 变量名:
    #格式:冒号不能丢
    file1.write('张无忌') 
    #格式:对文件的操作要缩进
    #格式:无需用close()关闭

So when you see the syntax format of opening a file with open...as, you should be calm. This is quite common.

As the saying goes, "What's the point of learning Python if you can't just write?" Next, let's write code together (p≧w≦q)!

small exercise

We have been here all the way, and you should have noticed that I really like "Harry Potter" haha.

Now imagine that you have come to the wizarding world. The final semester is coming soon, and Hogwarts School of Witchcraft and Wizardry is preparing to tally everyone's results.

What is the basis for selection? These are the students’ usual homework scores.

There is now a file called scores.txt, which contains the scores of several magic assignments for Hermione, Harry, Ron, and Malfoy.

Insert image description here

However, because some magic assignments are of a certain degree of difficulty, the professor does not force students to hand them in, so the number of times everyone turns in their assignments is not consistent.

Insert image description here
Here is the content of the file. You can create a new scores.txt in your computer to operate.

罗恩 23 35 44
哈利 60 77 68 88 90
赫敏 97 99 89 91 95 90
马尔福 100 85 90

I hope you will count the total scores of these four students' magic homework, and then write it into a txt file. Note that this entire exercise can only be done in Python.

Faced with this function, please think about it for 30 seconds and roughly think about how to implement it? Then click enter to continue.

Okay, a very rough idea should be: get the data in the txt file, then make statistics on the data, and then write it to the txt file. OK, let's get started right away.

First of all, there is no doubt that it must be opening a file. Do you remember what function you use?

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 

Next, it’s time to read the file. Generally speaking, we use the read() function, but here, we need to process the data of four people separately. We want to process it row by row, not as a whole, so when reading, we also want to read it row by row. Pick.

Therefore, we need to use a new function readlines(), which is "read line by line".

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file1.readlines()      
file1.close()
print(file_lines)

Use the print() function to print it and see how the content read by this method is displayed:

Insert image description here
As you see, readlines() will get a list from the txt file, and each string in the list is each line in scores.txt. And there is a newline \n symbol after each string.

In this way, we can use a for loop to traverse the list and then process the data in the list. Please see the fifth line of code:

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file1.readlines()
file1.close()

for i in file_lines:    #用for...in...把每一行的数据遍历
    print(i)            #打印变量i

Let's print it out as well.

Insert image description here
Okay, now we need to separate the name and score of each line here. At this time, we need to use split() to separate the string. It will separate the contents of the string by spaces.

Looking at Ron 23 35 44 in the first line of the picture above, it will be divided into ['Ron', '23', '35', '44'].

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file1.readlines()
file1.close()

for i in file_lines:   #用for...in...把每一行的数据遍历  
    data =i.split()    #把字符串切分成更细的一个个的字符串
    print(data)        #打印出来看看

The terminal looks like this:
Insert image description here
Obviously, comparing the pictures of the two terminals above, split() divides the content of each line into strings, so it becomes a list.

split() is a method for processing strings that we have not learned before. Here, the teacher would like to add that there are many methods for processing data types, but we cannot learn them all at once. Instead, we should learn the most basic and necessary methods. knowledge, and then continue learning when new knowledge is needed.

split() splits a string, and there is also a join() function that merges strings.

a=['c','a','t']
b=''
print(b.join(a))
c='-'
print(c.join(a))

operation result:

cat
c-a-t

The usage of join() is str.join(sequence). str represents what string you want to use to connect among these strings. Here are two examples, one is an empty string, the other is a horizontal bar, and sequence represents data. Sequence, in this case list a.

This is just to let everyone understand join(). It does not need to be memorized. You can just use it later and read it again.

Back to Harry Potter:

Insert image description here
The 0th data in these four lists is the name, and the following ones are the grades. We need to first count the total scores of each person, and then put the names and scores together.

You can still use a for...in... loop to perform addition operations. Please see the code on line 8:

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file1.readlines()
file1.close()

for i in file_lines:
    data =i.split()    
    sum = 0                    #先把总成绩设为0
    for score in data[1:]:     #遍历列表中第1个数据和之后的数据
        sum = sum + int(score) #然后依次加起来,但分数是字符串,所以要转换    
    result = data[0]+str(sum)  #结果就是学生姓名和总分
    print(result)

Let’s take a look at the terminal:

Insert image description here
Okay, the next step is to write the results into an empty list, because this will help us write a txt file later.

file1 = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file1.readlines()
file1.close()

final_scores = []                        #新建一个空列表
 
for i in file_lines:
    data =i.split()    
    sum = 0                    
    for score in data[1:]:     
        sum = sum + int(score)     
    result = data[0]+str(sum)+'\n'       #后面加上换行符,写入的时候更清晰。    
    final_scores.append(result)#每统计一个学生的总分,就把姓名和总分写入空列表

Okay, so we've already taken care of it, all we have to do is write it to the file. You can start reading from line 15:

file = open('/Users/Ted/Desktop/scores.txt','r',encoding='utf-8') 
file_lines = file.readlines()
file.close()

final_scores = [] 

for i in file_lines:
    data =i.split()    
    sum = 0                    
    for score in data[1:]:     
        sum = sum + int(score)     
    result = data[0]+str(sum)+'\n'    
    final_scores.append(result)

winner = open('/Users/Ted/Desktop/winner.txt','w',encoding='utf-8')   # 从这行开始看
winner.writelines(final_scores)
winner.close()

Line 15 of code opens a file called winner.txt. (If winner.txt does not exist on your computer, this line of code will automatically create a blank winner.txt for you)

The 16 lines of code are written using writelines(). Why can't write() be used? Because final_scores is a list, and the parameter of write() must be a string, and writelines() can be a sequence, we use writelines().

Line 17 of code closes the file. This, just remember not to throw away the parentheses.

In this small exercise, we have added some new function usages. They are not difficult, but they are still a little unfamiliar to you. However, they are actually related to the functions we have learned, or their usage is very similar. , you just need to be familiar with it and turn it out when you need to use it.

Moreover, compared to the several mountains we have already climbed, that is, the practical levels of the previous projects, this small exercise should only be regarded as a small hillside that can be easily climbed, right?

Okay, that’s the content of this level~ That’s it for the course. Don’t underestimate these two pieces of knowledge and review them more! Go do the exercises!

Guess you like

Origin blog.csdn.net/qq_41308872/article/details/132281545#comments_28450231