python文件操作(创建与读取)-创新互联

海丰网站建设公司成都创新互联公司,海丰网站设计制作,有大型网站制作公司丰富经验。已为海丰超过千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的海丰做网站的公司定做!>###############################创建文件########################################### #!/usr/bin/env python '''make a test file''' import os  ls = os.linesep #get file name fname = raw_input('Enter Filename: ') while True:     if os.path.exists(fname):         print('ERROR: %s already exists' % fname )         exit()         break     else:         break #get file content(test) lines all = [] print("Enter lines ('.' by itself to quit)") #loop until user terminates input while True:     entry = raw_input('> ')     if entry == '.':         break     else:         all.append(entry) #write lines to file with proper line-ending with open(fname,'w') as f:     f.writelines(['%s%s' %(x,ls) for x in all]) print 'DONE!' ###################################读取文件###################################### #!/usr/bin/env python '''read and display text file''' #get file name fname = raw_input("Enter filename: ") print #attempt to open file for reading try:     fobj = open(fname,'r') except IOError,e:     print('***file open error:',e) else:     #display content to the screen     for eachLine in fobj:         print eachLine     fobj.close()
文章题目:python文件操作(创建与读取)-创新互联
网站链接:http://myzitong.com/article/dsssig.html