Python检验一个文件是否存在,如果不在就自己填写内容。

  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  
posted @ 2015-02-04 18:50  Dus  阅读(317)  评论(0编辑  收藏  举报