try: file=open('aaaa','r+w') except Exception as e: print('there is no such file') response=input('do u want to creat?') if response=='y': file = open('aaaa','w') else: pass else: file.write('ssss') file.close() 我不管運行幾次都不會跳到寫入ssss那行,是否哪裡打錯了>
try: file=open('eeee','r+') except Exception as e: print('there is no file named as eeee') response=input('do you want to create a new file') if response == 'y': file=open('eeee','w') else: pass else: file.write('ssss‘) file.close 可是eeee里并没有写ssss诶
我试着自己做小改,但是运行了系统不报错也没出现按任意键结束的光标,想向您询问发生了什么情况,谢谢。 input_file = input('please type file name what you want to find: ') try: try_file = open(input_file,'r+') except Exception as e: print ('there is no file named as '+ input_file) response = input('do you want to create a new file? ') if response == 'y': file = open(input_file,'w') else: pass else: file.write('wow.cooooooooool') file.close
try: file = open('eeee','r+') except Exception as e: print("ther is no file named as eeee") response = input('do you want to create a new file') if response == 'y': file = open('eeee','w') else: pass else: file.write('ssss') file.close() 你好,请问下我照着打了,shell那显示出和你一样的指令,但是搜索系统却没有创建eeee的文件,请问下是什么问题?
使用2.7版本的Python,需要把input改成raw_input才能运行成功(小白笔记)
try:
file=open('aaaa','r+w')
except Exception as e:
print('there is no such file')
response=input('do u want to creat?')
if response=='y':
file = open('aaaa','w')
else:
pass
else:
file.write('ssss')
file.close()
我不管運行幾次都不會跳到寫入ssss那行,是否哪裡打錯了>
謝謝你~!!
清楚明瞭!給讚!
file.close()应该在else内写吧,否则如果若不创建文件的话,则没有file文件,也就无法运行file.close了...另一个问题是为何我不能用break代替pass呢
請問如何寫入每一次的錯誤訊息,如: No such file or directory: 'eeee.txt'而不是'ssss'
最后一行的file.close()是不是前面应该加上四个空格?不然如果response=no的话,由于没有打开文件,会抛出异常
如果不存在文件并选择y的话,文件依然没有关闭,应该在if == y 里面再关闭一下
try:
file=open('eeee','r+')
except Exception as e:
print('there is no file named as eeee')
response=input('do you want to create a new file')
if response == 'y':
file=open('eeee','w')
else:
pass
else:
file.write('ssss‘)
file.close
可是eeee里并没有写ssss诶
HELLO.你的file.close是否忘了加()括號呢??
要给eeee命名的 比如说你要在后面加上.py 或者.txt 什么的
第一遍运行完之后创建了eeee这个文件,之后再运行一遍才会写入ssss
一个是close后应该有(),另一个是此程序应该运行两边,一遍是创建一遍是添加
我翻墙看的好累,突然想着等网速好的时候把视频下载好能一直看,后来才发现优酷上也有……我要去哭一会
我试着自己做小改,但是运行了系统不报错也没出现按任意键结束的光标,想向您询问发生了什么情况,谢谢。
input_file = input('please type file name what you want to find: ')
try:
try_file = open(input_file,'r+')
except Exception as e:
print ('there is no file named as '+ input_file)
response = input('do you want to create a new file? ')
if response == 'y':
file = open(input_file,'w')
else:
pass
else:
file.write('wow.cooooooooool')
file.close
close(),即使这样可以建档,但也不能写入
try:
file = open('eeee','r+')
except Exception as e:
print("ther is no file named as eeee")
response = input('do you want to create a new file')
if response == 'y':
file = open('eeee','w')
else:
pass
else:
file.write('ssss')
file.close()
你好,请问下我照着打了,shell那显示出和你一样的指令,但是搜索系统却没有创建eeee的文件,请问下是什么问题?
我创建了eeee,可是没有写入ssss,也不知道为什么
請問file = open('eeee', 'r+')中的r+是甚麼意思?
read+write
你好,我想问一下那个except as的用法
***** 谢谢解答
file.close()不用縮進
有這個語句解決很多問題
python學習筆記本 ➡️ bit.ly/2pYRYLe
那还不如直接让open判断。。。
请问怎么用open判断