海豐網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,海豐網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為海豐超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的海豐做網(wǎng)站的公司定做!>###############################創(chuàng)建文件###########################################
#!/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()
網(wǎng)站題目:python文件操作(創(chuàng)建與讀?。?創(chuàng)新互聯(lián)
當前地址:
http://weahome.cn/article/dsssig.html