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'

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 !!

你帖子中的代码格式乱了。需要编辑一下。
错误提示也是说你代码对齐不对。

Python代码很大程度上是依赖于代码缩进的,你现在的代码一点都看不出缩进,你必须重新编辑下你的代码,一定要体现出缩进。
此外,
1. “import os” 这样的代码只需要在Py文件的开头引入就够了。
2. 如果你要问别人问题的话,最好将错误信息也贴出来。

就是说你的缩进有问题,类似C语言中的花括号不匹配。

http://www.crifan.com/python_syntax_error_indentationerror/

对于此错误,最常见的原因是,的确没有对齐,但是也可能是空格和Tab混用了
最好检查一下代码缩进

提示什么错误信息呢?是否是没有权限,检查下。