Python File IO
时间:2011-04-30 来源:qiang.xu
'''
Created on 2011-4-30
@author: xuqiang
'''
# open file
# open(file, mode)
# file : the file to be opened
# mode : open file mode, 'r' the file will only be read
# 'w' only writing and existing file with the same name will be erased
# 'a' opens the file for appending
# 'r+' read & write
# 'b' on windows, use this flag to indicate that open file binary, eg 'wb', 'r+b'
f = open('./file.txt', 'r+');
# read the first line
print(f.readline());
# read the left lines
print(f.readlines());
# tell the file pointer position
print("now the file position is ", f.tell());
# set the file pointer to 0L
f.seek(0);
# close the file
f.close();
Created on 2011-4-30
@author: xuqiang
'''
# open file
# open(file, mode)
# file : the file to be opened
# mode : open file mode, 'r' the file will only be read
# 'w' only writing and existing file with the same name will be erased
# 'a' opens the file for appending
# 'r+' read & write
# 'b' on windows, use this flag to indicate that open file binary, eg 'wb', 'r+b'
f = open('./file.txt', 'r+');
# read the first line
print(f.readline());
# read the left lines
print(f.readlines());
# tell the file pointer position
print("now the file position is ", f.tell());
# set the file pointer to 0L
f.seek(0);
# close the file
f.close();
相关阅读 更多 +