Python——打開了文件一定要關閉!

 

  1. #使用笨方法學Python遇到的第一個問題 —— 如何關閉打開的文件
  2. #這足以證明我的C都還給老師了_(:з」∠)_我都不好意思說我還學過C++還學過Android TuT
  1. from sys import argv 
  2.  
  3. script, filename = argv 
  4.  
  5. txt = open(filename) 
  6.  
  7. print "Here's your file %r:" % filename 
  8. print txt.read() 
  9.  
  10. print "I'll also ask you to type it again:" 
  11. file_again = raw_input("> "
  12.  
  13. txt_again = open(file_again) 
  14.  
  15. print txt_again.read() 
  16.  
  17. txt.close() #關閉打開的文件
  18. txt_again.close() 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章