wo_calecaca 2015-04-04 05:35 采纳率: 100%
浏览 1946
已采纳

python简单读写文件问题代码纠错

def write(self,fname):
import os
ls = os.linesep
while True:
if os.path.exists(fname):
print "ERROR:'%s'already exists"%fname
break
else: break
all=[]
print"\nEnter lines('.'by itself to quit).\n"
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!'

def read(self,fname):
print
try:
fobj=open(fname,'r')
except IOError,e:
print"***file open error:",e
else:
for eachline in fobj:
print eachline
fobj.close()

import os
way=raw_input("choose you want to do Write or Read:")
if way==Write:
write()
elif way==Read:
read()
else:print 'ERROR'

展开全部

  • 写回答

6条回答 默认 最新

  • Landpack 2015-04-06 04:06
    关注

    example:

     #!/usr/bin/python
    
     def hello():
    
         print 'Hello,World'
    
    if __name__ == '__main__':
        hello():
    
    

    But if you do it like this below ..

     #!/usr/bin/python
    
     def hello()
    
     print 'Hello,World'
    
     if __name__ == '__main__':
    
     hello()
    
    

    You will see those error :

    图片说明

    Good Luck !!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?