Created
March 23, 2012 13:31
-
-
Save rswofxd/2170640 to your computer and use it in GitHub Desktop.
File文件操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
#..........file.............. | |
#open(filename,mode),mode is optional,and return a file object | |
f = open('/xx/xx/file','r/w/r+/a') | |
f.read(size) | |
f.read(line) | |
f.read(lines) | |
for line in f: #all-list the file content | |
print(line,end=' ') | |
value = ('answer',100) | |
s = str(value) #to translate to string | |
f.write(s) #f.write(string),and return a lenth | |
#f.seek(offset,from_what),from_what=0,1,2,and 0 is begining,1 is current,2 is end,and the file must be b-bite,or from_what=0 | |
f.seek(-3,2) | |
#pickle is a module,meaning packing any type data | |
pickle.dump(x,f)#pickle object x to file f,to translate to string | |
x = pickle.load(f)#to unpickle object x from f | |
f.close() #to close the opened file,and release the memery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment