current position:Home>A glance at Python's file processing (Part 1)

A glance at Python's file processing (Part 1)

2022-01-31 12:45:31 Lei Xuewei

「 This is my participation 11 The fourth of the yuegengwen challenge 10 God , Check out the activity details :2021 One last more challenge

ceremonial Python Column No 34 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

A lot has been written before , Are doing some data processing , But when we need to store and read data , You need documents . The school committee took you through file processing .

First learn to read and write documents !

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

This requires the use of files to handle ( You can also use a database , But in general C Language is the preferred language for many freshmen in computer department , At this time, the probability does not know the database ).

python The most common is open and write function , as follows :

#open function : Receive a file name , There are other parameters that can be omitted .
one_file = open('myfile.txt')
# Read data assigned to data Variable 
data = one_file.read()

# Of a file object write function 
one_file = open('myfile.txt','w')
#write function : Incoming data ,write Function writes data to one_file In the corresponding file .
one_file.write(' Data written to a file ')
 Copy code 

Let's take a look at some examples of file operations

Read file data

Keep the following data as a file :sample.txt

 Continuous learning 
 Continuous development 
 My name is Lei Xuewei 
 Copy code 
afile = open("sample.txt")
print(afile.read())
 Copy code 

 Screenshot  2021-11-13  In the morning 12.07.35.png

Write data and simply display

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/12 11:58  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : filedemo.py
# @Project : hello

afile = open("sample.txt")
data = afile.read()
print("sample file content=", data)
afile.close()

# hold sample The read data is written to test file 
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 code 

Let's see , The read content is indeed written .

The operation of this file is very simple .

 Screenshot  2021-11-13  In the morning 12.19.19.png

Reading and writing files is so simple , But after we manipulate the file , Remember to call close function ( Close file , Otherwise, exceptions will occur in subsequent read / write operations / error !)

close The function is called as follows :

one_file.close() # File object .close()
 Copy 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 了 , This with Within code block ,python Will help us automatically close the file .
 Copy code 

All the above are read at one time as soon as you open the file ,Python You can also read line by line .

According to the line read / Write data by line

According to the line read

We modified the code based on the previous read-write file , Look directly at :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/12 11:58  Afternoon 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : filedemo2.py
# @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 code 

We see here reading sample One line of the file , Then write to test file , There is only one line !

 Screenshot  2021-11-13  In the morning 12.40.52.png

summary

Python The reading of files is very convenient , Built in open Functions and file objects come with write function , The design is very simple .

Open the box , So simply knock , Take a few minutes to learn some file operations !

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311245299349.html

Random recommended