python--writefile&readfile

4731 단어

writefile
#!/usr/bin/env python
'makeTextFlie.py --create text file'
import os
ls = os.linesep
#get filename
fname = raw_input('input your file name:
') while True: if os.path.exists(fname): print "error: '%s' already exists
" % fname else: break #get file content lines all = [] #get list print "enter line ('.'by itself to quie).
" #loop until user terminates input while True: entry = raw_input('> ') if entry == '.': break else: all.append(entry) fobj = open(fname, 'w') fobj.writelines(['%s%s'%(x, ls)]for x in all) fobj.close() print 'DONE'

readfile
#!/usr/bin/env python
'readTextFlie.py --create text file'
import os
ls = os.linesep
#get filename
fname = raw_input('input your file name
') try: fobj = open(fname, 'r') except IOError, e: print 'open file error:
',e else: for eachline in fobj: print eachline, fobj.close()

 
다음으로 전송:https://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/08/10/2632005.html

좋은 웹페이지 즐겨찾기