Any language is inseparable from the operation of files , that Python How languages operate and manage files .
Encoding mode
The history of coding is roughly ASCII ->gb2312->unicode->utf-8, During the specific details can baidu
Let's take a little example of encoding and decoding , Remember that Chinese can be done GBK and utf-8 code , stay GBk One Chinese character corresponds to two bytes , stay utf-8 A Chinese character corresponds to three bytes , Chinese can't be done ASCII code .
If you have difficulties in learning , Looking for one python Learning communication environment , Can join us python circle , Skirt number 947618024, Can claim python Learning materials , It will save a lot of time , Reduce a lot of problems .
If the encoding and decoding format is inconsistent, there may be garbled code ,encode It means code ,decode It means decoding .
File operated API
Here is Python File operation specific API.
Method meaning open open read Read write write in close close readline Single line read readlines Multi line read seek File pointer operation tell Read the current pointer position
Open file
Python Of open() Function to open a file , There are several parameters available . However , The most common parameters are the first two .
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Be careful , The first is mandatory , The rest is optional . If you do not add mode Parameters , The file will be Python Open in read-only mode .
encoding: Don't write . Don't write parameters , The default codebook is the default codebook of the operating system .windows Default gbk,linux Default utf-8,mac Default utf-8.
mode
mode meaning r Text mode , Read rb Binary mode , Read w Text mode , write in wb Binary mode , write in a Text mode , Additional ab Binary mode , Additional + Can read but write
Read the file
Code used in the file file operation of 1.txt The contents of the document are as follows :
readline(), When this method is used , You need to specify the mode to open the file as r perhaps r+;
readlines(), Read all lines . Return a list , Each element in the list is each line of the original file . If the file is large , Occupy memory , It's easy to collapse .
write file
The following is only about clear write w And add in a
Case study : Write attention programming a meow into test.txt In file
Next, the author : Programming a meow to write test.txt In file
After running, you will find that w Mode will clear the contents of the original file , So use it with care . Just use w, It's going to be done all at once .
Additional writing a
Case study : Add the poem of silent night thinking to test.txt In file
Pointer operation
Things or resources exist in the form of documents , Like news 、 Shared memory 、 Connections etc. , Handles can be understood as pointers to these files .
Handle (handle) It's a term from compiler principles , Refers to the part of a sentence that is first regulated , So with a 「 sentence 」 word .
The function of a handle is to locate , Two APi still tell and seek.
tell Returns the current location of the file object in the file ,seek Move the file object to the specified location , The parameter passed in is offset , Represents the offset of the movement .
The following is an example to further understand the above functions , As shown below :
Context management
We're going to do this : Open file , Reading and writing , Close file . Programmers often forget to close files . The context manager can be used when files are not needed , Close files automatically , Use with open that will do .
How to read multiple files in batch
below , Batch read a folder under txt file
Next, we will read the folder in batches txt The content of the document , Merge content into a new file 5.txt in , The specific implementation code is as follows .
Actually in Window We only need cd To the destination folder , That is, you need to merge all the txt Add the file to the destination folder , The implementation is as follows DOS command type *.txt > C:\ The target path \ The merged file name .txt
practice
subject : create a file data.txt, Documents in total 100000 That's ok , One for each line 1~100 Integer between , Title source : Cattle guest
subject : Generate 100 individual MAC Address and write it to the file ,MAC Before the address 6 position (16 Base number ) by 01-AF-3B, Title source : Cattle guest
One more sentence at the end , Want to learn Python Please contact Xiaobian , Here's my own set python Learning materials and routes , Anyone who wants this information can enter q skirt 947618024 receive .
The material of this article comes from the Internet , If there is infringement, please contact to delete .