File processing
F=open (' file ',' R ') to open a file
F.close () close file, equivalent to save
1. character encoding
In the process of file processing, first there is an important concept, is the character encoding
Character encoding: Compiling human characters into numbers that computers can recognize
Character encoding table: A table that corresponds to a character and a number
Ascii
Gbk
Utf-8 (Common encoding on hard disk)
Unicode (commonly used encoding in memory)
Unicode---->encode (' utf-8 ') encoding ----> bytes
Bytes----> decode (' Utf-8 ') decoding ----> Unicode
Principle: What format the character is compiled in, and what format it will decode
the strings in Python3 are divided into two types
x= ' Egon ' default to Unicode
Y=x.encode (' Utf-8 ') uses encode encoding for bytes
There are two types of strings in Python2
X=u ' Egon ' plus u means to be stored in Unicode format, as with Python3 string
Y= ' Alex ' is saved as bytes by default
2. File Mode
R Text mode read, the file does not exist, the new file is not created
W text mode write , the file exists is emptied, does not exist then creates
A text pattern is appended , the file exists cursor jumps to the end of the file, the file does not exist created
RB reads bytes directly from the hard drive
WB opens in binary write mode
AB opens in binary append mode
r+ can write when he reads.
w+ is readable when written.
A + can be read and written when appended
A. r mode
F.read () # reads all the contents of the text once and returns the result as a string
use of read ():
Read (3) # file opens in text mode, represents reading 3 characters
# when the file is opened in B mode, it represents a read of 3 bytes
3- byte =1 characters in a #unicode格式
Controls the movement of the cursor (all in bytes):
F.seek () # control cursor moves several bytes
There are three types of modes:
0 starting with the first byte
Run in 1 B mode with reference to the current cursor position
2 b mode operation, with the last byte as the reference object
exercise: Simulating Tail–faccess.log
# Python3 Tail.py-f access.log
ImportTime
ImportSYS
withOpen(R '%s '% sys.argv[2], ' RB ') asF:
F.seek (0, 2)
While True:
line = F.readline ()
ifLine :
Print(Line.decode (' Utf-8 '),End="')
Else:
Time.sleep (0.2)
Truncate () # file opens as writable, starting at the beginning of the file, leaving bytes after the specified byte
F.readline () # reads only the contents of the first line of text and returns the result as a string
F.readlines () # reads all the contents of the text and returns the result in the form of a series, typically with the for in
F.readble () # Determines whether a file has Read permission, returns a Boolean value
B. W mode
F.write () # write to file, newline requires manual write line break \ n
F.writelines () # write multiple lines at once
C. A mode
F.tell () # view cursor position
With open (' file ', ' W ', encoding= ' utf-8 ') as F:
F.write (' 11111\n ') # will assign the contents of the file to F, and the execution will automatically close
In Python, you pass the script's arguments to the method inside the script:
Import Sys
Print (SYS.ARGV)
a python small script for a CP file
ImportSYS
#python3 copy.py source.file target.file
ifLen(SYS.ARGV) <3:
Print(' Usage:python3 copy.py source.file target.file ')
Sys.exit ()
#r ' C:\Users\Administrator\PycharmProjects\python18Weekend Class \day3\test.jpg '
withOpen(R '%s '%sys.argv[1],' RB ') asRead_f,\
Open(R '%s '%sys.argv[2],' WB ') asWrite_f:
for LineinchRead_f:
Write_f.write (line)
This article is from the "Lyndon" blog, make sure to keep this source http://lyndon.blog.51cto.com/11474010/1948655
Python Basic---file processing