“This is the 10th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Official Python column # 34, stop! Don’t miss this zero-based article!

We’ve done a lot of stuff, we’re doing some data processing, but when we need to store and read data, we need files. This session will take you through document processing.

Learn to read and write files first!

For example, when I was in school, the design requirement of the first programming course was to make a student management system.

This requires the use of files to deal with (you can also use a database, but the general C language is the first language of many freshmen in the computer department, at this time the probability is also unaware of the database).

The most commonly used python functions are the open and write functions, as follows:

The #open function accepts a file name and other parameters that can be omitted.
one_file = open('myfile.txt')
Read data and assign it to the data variable
data = one_file.read()

Write function for a file object
one_file = open('myfile.txt'.'w')
The write function writes the data to the one_file file.
one_file.write('Data written to file')
Copy the code

Let’s look at some file manipulation examples

Reading file data

Save the following data in the file sample.txt

Continuous learning and continuous development of my Thunder committeeCopy the code
afile = open("sample.txt")
print(afile.read())
Copy the code

Simple presentation of write data

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # hello afile = open("sample.txt") data = afile.read() print("sample file content=", Afile = open("./test.txt", "w") afile.write(data) afile.close() print("write data to test file!" ) afile = open("./test.txt") data = afile.read() afile.close() print("test file content=", data)Copy the code

Let’s see, what we read is actually written.

This file operation is very simple.

Reading and writing files is as simple as that, but after we manipulate the file, remember to call close (close the file, otherwise there will be exceptions/errors in subsequent reading and writing operations!).

The close function is called as follows:

one_file.close() # file object.close ()
Copy the code

But we usually write this style of file reading and writing:

with open('sample.txt'.'r') as one_file:
    data = one_file.read()
# No need to call close, python will automatically close the file for us inside the with block.
Copy the code

All of the above are read once the file is opened, and you can read it line by line in Python.

Read/write data line by line

According to the line read

Read and write file code based on the previous modification, directly look at:

#! /usr/bin/env python # -*- coding: utF-8 -*- # @author: xuewei # @csdn /Juejin/Wechat: # @XueWeiTag: CodingDemo # @XueWeiTag: CodingDemo # @Project: Hello with open("sample.txt") as afile: data = afile.readline() print("sample file content=", data) with open("./test.txt", "w") as afile: afile.write(data) print("write data to test file!" ) with open("test.txt") as afile: data = afile.readline() print("test file content=", data)Copy the code

We see here that we read one line from the sample file and then write it to the test file, which is only one line!

conclusion

Python files are very easy to read. The built-in open function and the built-in write function for file objects are very simple in design.

Out of the box, so tap away and take a few minutes to learn some file manipulation!

By the way, if you like Python, please check out the Committee’s Python Basics column or Python Getting Started to Master column

Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!