Python檢驗一個檔案是否存在,如果不在就自己填寫內容。

Dus發表於2015-02-04
  1. import os  
  2. while True:  
  3.     filename=input('Please enter the filename')  
  4.     if os.path.exists(filename):  
  5.         print('the file is exist')  
  6.         break  
  7.     else:  
  8.         all=[]  
  9.         while True:  
  10.             content=input('>')  
  11.             if content=='.':  
  12.                 print('Exit')  
  13.                 break  
  14.             else:  
  15.                 all.append(content)  
  16.         fobj=open(filename,'w')  
  17.         str_list=[i+'\n'for i in all]  
  18.         fobj.writelines(str_list)  
  19.         fobj.close()  
  20.         break  

相關文章