Introducing Python Files

本篇将介绍一下 Python 内置的 Files 类型,通过实例学习一下常用的文件操作。

Methods

$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

# open txt file for both writing and reading
>>> foo = open("/Users/leehenry/backup/fool.txt", "w+")
>>> foo
< open file 'fool.txt', mode 'w+' at 0x11003b780 >

# get name of the file
>>> foo.name
'fool.txt'

# access mode with which file was opened
>>> foo.mode
'w+'

# true if file is closed, false otherwise
>>> foo.closed
False
>>> foo.close()
>>>
>>> foo.closed
True

Tips

Files 常用的文件操作。

# open and write
>>> foo = open("/Users/leehenry/backup/fool.txt", "wb")
>>> foo.write("Tell me \nRuby and Python \nWhich one do you like best")
>>> foo.close()

# open and read
>>> foo = open("/Users/leehenry/backup/fool.txt", "r")
>>> foo.read()
'Tell me \nRuby and Python \nWhich one do you like best'

# File position important!
# tell() method tells you the current position within the file
>>> foo.tell()
52
>>> foo.read()
''

# seek(offset[, from]) method changes the current file position
>>> foo.seek(0)
>>> foo.read()
'Tell me \nRuby and Python \nWhich one do you like best'

# readline()
>>> foo.seek(0)
>>> foo.readline()
'Tell me \n'

# readlines()
>>> foo.seek(0)
>>>
>>> for line in foo.readlines():
...   print line
...
Tell me

Ruby and Python

Which one do you like best

2014-05-20

rocket-wing