[python]基本文件操作
1. file_object = open(file_name, access_mode='r', buffering=-1)
关于acess_mode的解释
File Mode Operation
r Open for read
rU or U Open for read with universal NEWLINE support (PEP 278)
w Open for write (truncate if necessary)
a Open for append (always works from EOF, create if necessary)
r+ Open for read and write
w+ Open for read and write (see w above)
a+ Open for read and write (see a above)
rb Open for binary read
wb Open for binary write (see w above)
ab Open for binary append (see a above)
rb+ Open for binary read and write (see r+ above)
wb+ Open for binary read and write (see w+ above)
ab+ Open for binary read and write (see a+ above)
+ is for read-write access
2. 任何使用open()的地方都可以使用file()代替
Generally, the accepted style is that you use open() for reading/writing files, while file() is
best used when you want to show that you are dealing with file objects, i.e., if instance(f, file).
3. seek(pos, whence=0) 没有返回值
seek() moves the file pointer to different positions within the file.The offset in bytes is given
along with a relative offset location, whence. A value of 0, the default, indicates distance from
the beginning of a file (note that a position measured from the beginning of a file is also known
as the absolute offset), a value of 1 indicates movement from the current location in the file,
and a value of 2 indicates that the offset is from the end of the file.
tell()可以返回目前所在的位置 特可以配合seek()使用
4.
OS Module Attributes to Aid in Multi-platform Development
Attribute Description
linesep String used to separate lines in a file
sep String used to separate file pathname components
pathsep String used to delimit a set of file pathnames
curdir String name for current working directory
pardir String name for parent (of current working directory)
5. 除了file, os和os.path提供了一系列操作文件目录的方法。
浙公网安备 33010602011771号