wo_calecaca 2015-04-04 13: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 12: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条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制