Python检验一个文件是否存在,如果不在就自己填写内容。
- import os
- while True:
- filename=input('Please enter the filename')
- if os.path.exists(filename):
- print('the file is exist')
- break
- else:
- all=[]
- while True:
- content=input('>')
- if content=='.':
- print('Exit')
- break
- else:
- all.append(content)
- fobj=open(filename,'w')
- str_list=[i+'\n'for i in all]
- fobj.writelines(str_list)
- fobj.close()
- break
感谢关注