Categories
Uncategorized

Python 文件操作

Python  读取文件

file_object = open(‘thefile.txt’)
try:
     all_the_text = file_object.read( )
finally:
     file_object.close( )
读文本文件
input = open(‘data’, ‘r’)
#第二个参数默认为r
input = open(‘data’)

读二进制文件
input = open(‘data’, ‘rb’)

写文本文件
output = open(‘data’, ‘w’)

写二进制文件
output = open(‘data’, ‘wb’)

追加写文件
output = open(‘data’, ‘w+’)

写数据
file_object = open(‘thefile.txt’, ‘w’)
file_object.write(all_the_text)
file_object.close( )

http://www.cnblogs.com/allenblogs/archive/2010/09/13/1824842.html

Leave a Reply

Your email address will not be published. Required fields are marked *