Articles Catalogue
File operation
Common file types
- txt
- csv
- json
- html
- mp4
The flow of file operation
- Open - > open ()
- Read/Write - - > read ()/write ()
- Close - - > close ()
purpose | Method | function |
---|---|---|
Open/close files | f=open(file) | Open the file |
close() | Close files | |
Read file content | f,read([size]) | Read the specified number of bytes from the file; if not specified, read all. |
f,readline([size]) | Read the whole line, including "n" | |
f.readlines([size]) | Read all rows and return the list | |
f,write(str) | Write a string to a file and return the length of the string | |
f.writelines(seq) | Write a list of sequence strings to the file. If you need a newline, you need to add a newline character to the byte set | |
f.flush() | Refresh the file buffer and write the internal buffered data directly to the file | |
File Location | f.seek(offest) | Set the current location of the file, that is, the pointer location |
f.tell() | Returns the current location of the file, that is, the pointer location |
Opening and Closing of Files
open
Built-in function: open(), create a file object, and then use it to call related methods
Format:
file_object = open(file, [,mode], [,encoding='utf-8'], [buffering])
Explanation:
- file_object - > file object returned by open ()
- file - > Files to Open
- Mode - > Specified mode: Read-only, write-in, append, etc. Table. The default is read-only.
- Encoding - > Specify file encoding, usually, "utf-8" / "GBK"
- buffering - > If set to 0, there will be no registry, set to 1, when accessing files, the registry line will be registered; set to more than 1, indicating the buffer size of the registry; set to complex, then the buffer size of the registry is the default of the system.
Pattern | describe |
---|---|
r | Open the file read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
rb | Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. |
r+ | Open a file for reading and writing. The file pointer will be placed at the beginning of the file. |
rb+ | Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. |
w | Open a file for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file. |
wb | Open a file in binary format for writing only. If the file already exists, it is overwritten. If the file does not exist, create a new file. |
w+ | Open a file for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file. |
wb+ | Open a file in binary format for reading and writing. If the file already exists, it is overwritten. If the file does not exist, create a new file. |
a | Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is to say, the new content will be written after the existing content. If the file does not exist, create a new file for writing. |
ab | Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is to say, the new content will be written after the existing content. If the file does not exist, create a new file for writing. |
a+ | Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. File opens in additional mode. If the file does not exist, create a new file for reading and writing. |
ab+ | Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing. |
Properties of file objects
attribute | describe |
---|---|
f.name | Return file name |
f.mode | Returns access mode for open files |
f.closed | If the file is closed, return true; otherwise return false |
Close
f.close()
Save computer resources and provide performance. Close the file after you use it.
Reading and Writing of Documents
read
-
f.read([size])
- Read from the pointer to the file
- Pass size, specifying the number of bytes read
-
f.readline()
-
f.readlines
f = open('./data/GreenFlowerPorcelain.txt', 'r') f1 = f.readline() f2 = f.readline() f3 = f.readline() print(f1) print(f2) print(f3) fls = f.readlines() print(fls) print(len(fls)) f.close 1 Green Flower Porcelain 2 You are the face that has changed my whole world. 3 You are the face that I see everywhere I go. [" 4 You are so beautiful to me that I can't explain ,\n", ' 5 Just like a green flower porcelain.\n', " 6 You're like a moon that I awaken to say hello,\n", ' 7 So beautiful and bright that you make me content to play it so.\n', ' 8 I see your face on the leaves,telling me how lonely I have been.\n', ' 9 This is a dream of mine that I have just dreamed.\n', '10 Just see your smiling face everywhere I go.\n', '11 The love I feel for you to shine inside me.\n', "12 But it's all over now you're gone."] 9
f = open('./data/GreenFlowerPorcelain.txt', 'r') f1 = f.readline(10) f2 = f.readline(10) print(f1) print(f2) f.close() 1 Green F lower Porc
f = open('./data/GreenFlowerPorcelain.txt', 'r') fls = f.readlines() print(type(fls)) print(len(fls)) f.close() <class 'list'> 12
f = open('./data/GreenFlowerPorcelain.txt', 'r') for i in f: print(i, end='') #Remove line breaks (1) f = open('./data/GreenFlowerPorcelain.txt', 'r') for i in f: print(i.strip()) #Remove line breaks (2)
write
- write()
- writelines()
f = open('./data/City College.txt', 'w', encoding='utf = 8') f.write('I am from Zhejiang Province.') f.write('I am') #It's not coverage, it's additions. f.close()
f = open('./data/City College.txt', 'w', encoding='utf = 8') f.write('I am from Zhejiang Province.') f.close() f = open('./data/City College.txt', 'w', encoding='utf = 8') f.write('I am') f.close() # This is coverage.
f = open('./data/City College.txt', 'w', encoding='utf-8') seq = ['Zhejiang University City College\n','In Gongshu'] f.writelines(seq) f.close()
Location reading
f.tell()
- Get the current location of the file
f.seek()
- seek(offset[,from])
- offset, the number of bytes to move
- from, specify the reference location of the offset
- 0-> Start from scratch migration
- 1 -> offset from current position
- 2 -> Shift from the End Position*
f = open('./data/GreenFlowerPorcelain.txt', 'r') f1 = f.read(10) f_sl = f.tell() print(f_sl) f.seek(20, 0) f2 = f.read(20) f_sl2 = f.tell() print(f_sl2) print(f2) f.close() 10 41 elain 2 You are the
with open('./data/GreenFlowerPorcelain.txt', 'r') as f: print(f.read()) #It also closes the file at the end = f.close()
Randomly generate 100 integers of 1-100 and write them into files
import random f = open('./data/test.txt','w+') for i in range(100): num = random.randint(1,100) sr = str(num) f.write(sr+'\n') f.seek(0,0) print(f.read()) f.close()
os module
operator system