open(filename,mode,buffer)
The first parameter is the filename of the file to be opened, mandatory; the second is the opening mode, optional; the third is the buffer, optional. The default is to "read" mode to open the file. The function returns an object of type Stream.
There are several types of modes:
r: read mode (default)
w: Write mode
a: Additional write mode
b: Binary mode
t: text mode (default)
+: Update existing hard disk files (read and write mode)
U: Universal new line mode (UNLM)
If to open file does not exist or other problems, it will run an IOError exception.
Commonly used file object properties:
mode: file opening mode
name: name of the open file
closed: whether the file is closed or not
The common file object methods are:
tell(): get the current position in the file. The starting value is 0.
seek(position,mode): move in the current file. The first parameter is the distance to be moved, the second parameter is the mode: 0 means to move the absolute position, relative to the head of the file; 1 means to move the relative position, in terms of the current position; 2 means the position relative to the end of the file.
read(max_byte_num): read bytes from the file. max_byte_number is an optional parameter indicating the maximum number of bytes to read. If not selected, the default is to read to the end of the file. After reading, the current position will be changed, that is, the number of bytes read will be increased.
readline(): read the file one line at a time.
write(content): write data to the file. content is the content to be written.
close(): Close the file.
An example of a file read and write:
try:
f = open('d:/hello_python.txt','w')
('hello my friend python!')
except IOError:
print('IOError')
finally:
()
try:
f = open('d:\hello_python.txt','r')
print(())
()
()
except ValueError as ioerror:
print('File alread closed {0}'.format(type(ioerror)))
finally:
print('operation end')
The first parameter is the filename of the file to be opened, mandatory; the second is the opening mode, optional; the third is the buffer, optional. The default is to "read" mode to open the file. The function returns an object of type Stream.
There are several types of modes:
r: read mode (default)
w: Write mode
a: Additional write mode
b: Binary mode
t: text mode (default)
+: Update existing hard disk files (read and write mode)
U: Universal new line mode (UNLM)
If to open file does not exist or other problems, it will run an IOError exception.
Commonly used file object properties:
mode: file opening mode
name: name of the open file
closed: whether the file is closed or not
The common file object methods are:
tell(): get the current position in the file. The starting value is 0.
seek(position,mode): move in the current file. The first parameter is the distance to be moved, the second parameter is the mode: 0 means to move the absolute position, relative to the head of the file; 1 means to move the relative position, in terms of the current position; 2 means the position relative to the end of the file.
read(max_byte_num): read bytes from the file. max_byte_number is an optional parameter indicating the maximum number of bytes to read. If not selected, the default is to read to the end of the file. After reading, the current position will be changed, that is, the number of bytes read will be increased.
readline(): read the file one line at a time.
write(content): write data to the file. content is the content to be written.
close(): Close the file.
An example of a file read and write:
Copy Code The code is as follows.
try:
f = open('d:/hello_python.txt','w')
('hello my friend python!')
except IOError:
print('IOError')
finally:
()
try:
f = open('d:\hello_python.txt','r')
print(())
()
()
except ValueError as ioerror:
print('File alread closed {0}'.format(type(ioerror)))
finally:
print('operation end')