Python Learning Diary File Operations

Keywords: PHP encoding

File operations:

First, you need to have a file as an object, for example,'filename'.txt

1. File Path: Example d:\Filename.txt

<1>: Absolute path: the path from the root directory

<2>: Relative paths: What are the paths in the current directory?

2. Encoding method: UTF-8, GBK2312, etc.

3. Operation mode: Read-only, Write-only, Append, Write-read, Read-write, etc.

 

Open files in what encoding and how to store them

f = open('Target File',mode='r',encoding='utf-8')  #Take UTF-8 encoded files as an example
content = f.read()
print(content)
f.close()

Read:

1.mode ='r'file read by default

When the f.read() file is opened, the bytes type is converted to STR type, the return value is str, and the encoding under Python3 is unicode.

f2 = open('f:\Target File.txt',mode='r',encoding='gbk')  #The open file code is gbk
content2 = f2.read()
print(content2) #asd123 file
f2.close()

2.mode ='rb'is mainly used for non-text type file operations, without writing or encoding

3. Read before write

f = open('Target File',mode='r+',encoding='utf-8')
print(f.read())                 #123456789
f.write('abcd123')
f.close()                       #123456789abcd123

Add a print(f.read()) after f.write(), and this code will not execute

Write and read in r+mode, and cover as much as you write

#Original File Content:123456789
f = open('Target File',mode='r+',encoding='utf-8')
f.write('abcdefg')
print(f.read())     #89
f.close()

4.mode ='r+b'opens as bytes

#Original File Content:123456789
f = open('Target File',mode='r+b')
print(f.read()) #b'123456789'
f.write('abc'.encode('utf-8'))
f.close()   #After file update:123456789abc with utf-8 Code

Write:

If a new file is not created when a file is written, delete the original file and add a new one

1. Read-only mode ='w'

#Original File Content:123456789
f = open('Target File',mode='w',encoding='utf-8')
f.write('abcdefg')
f.close()   #After file update:abcdefg

2.mode = 'wb'

#Original File Content:123abc China
f = open('Target File',mode='wb')
f.write('gbka123'.encode('utf-8'))
f.close()   #After file update:gbka123  Encoding Method:utf-8

If you read this file in gbk encoding, you will get an error

3.mode ='w+'write before read

#Original File Content:123abc China
f = open('Target File',mode='w+',encoding='utf-8')
f.write('gbk2312')
print(f.read())
f.close()   #After file update:gbk2312  Encoding Method:utf-8

4.mode = 'w+b' 

f1 = open('logo-grey.png',mode='rb')
content = f1.read()
print(content)
f1.close()
f2 = open('logo-grey2.png',mode='wb')
f2.write(content)
f2.close()

Read the original file as rb and write it to a new file

Append:

1.mode = 'a'

#Original File Content:123abc China
f = open('Target File',mode='a',encoding='utf-8')
f.write('%%%%')
f.close()   #After file update:123abc China%%%%  Encoding Method:utf-8

2.mdoe = 'a+'

#Original File Content:123abc China
f = open('Target File',mode='a+',encoding='utf-8')
f.write('%%%%')
print(f.read())
f.close()   #After file update:123abc China%%%%  Encoding Method:utf-8

Other functions:

1.f.read(n)

Represents reading n characters when the file is opened in text mode

#Original File Content:123abc China
f = open('Target File',mode='r',encoding='utf-8')
content = f.read(6)
print(content)
f.close()   #123abc 

Represents reading n bytes when the file is opened in b mode

f = open('logo-grey.png',mode='rb')
content = f.read(10)
print(content)
f.close()   #b'\x89PNG\r\n\x1a\n\x00\x00'

2.f.seek (cursor position)

The search (n) cursor moves to the N position in byte, and all utf-8 encoded Chinese parts must be multiples of 3

Move to the beginning: seek(0)

Move to End: seek(0,2) seek The last parameter indicates from which location to offset, 1 represents the front position, 2 represents the end

#Original File Content:123abc China
f = open('Target File',mode='r+',encoding='utf-8')
f.seek(0)       #Place the cursor at the beginning
print(f.read())
f.seek(0)       #Place the cursor at the beginning
f.seek(0,2)     #Place the cursor at the end
print(f.read()) #Read blank
f.seek(0)
f.write('casual')   
f.flush()       #After file update:China at will 3*2 = 6 byte
f.close()

3.f.tell()

Tell us where the cursor is

#Original File Content:123abc China
f = open('Target File',mode='r+',encoding='utf-8')
f.seek(0)       #Place the cursor at the beginning
print(f.tell()) #0
print(f.read())
f.seek(0)       #Place the cursor at the beginning
print(f.tell()) #0
f.seek(0,2)     #Place the cursor at the end
print(f.tell()) #12
print(f.read()) #Read blank
f.seek(0)
print(f.tell())
f.write('casual')
f.flush()       #After file update:China at will 3*2 = 6 byte
print(f.tell()) #6
f.close()

4.f.readable()&f.writeable() to determine readability

#Original File Content:123abc China
f = open('Target File',mode='r+',encoding='utf-8')
print(f.readable())
print(f.writable())
f.close()

5.f.readline()

Read only one line at a time, the read data has a'\n', the original file content will not change

#Original File Content:  
# 123
# abc
# gb2
# 456
# asd
import os
li = []
f = open('Target File',mode='r+',encoding='utf-8')
while f.tell() != os.path.getsize('Target File'):
    li.append(f.readline().strip())
f.close()
print(li)   #['123', 'abc', 'gb2', '456', 'asd']

6.f.readlines()

Returns a list of elements that are each line of the original file. Files that are too large can take up memory and crash easily

#Original File Content:
# 123
# abc
# gb2
# 456
# asd
f = open('Target File',mode='r+',encoding='utf-8')
print(f.readlines())    #['123\n', 'abc\n', 'gb2\n', '456\n', 'asd']
f.close()

7.f.truncate()

Intercept data from original file, 0 deletes all intercepts in byte

#Original File Content:123456abcd
f = open('Target File',mode='r+',encoding='utf-8')
f.truncate(4)
print(f.read()) #1234
f.close()

8.for loop

A file handle is an iterator that saves memory by only one row of data per loop

#Original File Content:
# 123
# abc
# gb2
# 456
# asd
f = open('Target File',mode='r+',encoding='utf-8')
for i in f:
    print(i,end = '')
f.close()
# print contents
# 123
# abc
# gb2
# 456
# asd

9. Another way to open files

with open('Target File',mode='r',encoding='utf-8') as f:
    print(f.read())

Multiple file operations:

with open('Target File',mode='r',encoding='utf-8') as f1,\
        open('Target File 2',mode='w',encoding='utf-8') as f2:
    print(f1.read())
    f2.write('something')

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--End of recovery content--

Posted by cruz610 on Wed, 31 Jul 2019 14:43:11 -0700