파일을 쓰기 전에 파일이 있는지 확인하는 방법
- import os
- if not os.path.exists('filename'):
- with open('filename', 'wt') as f:
- f.write('This is a test')
- else:
- print('File exists')
실수로 파일을 덮어쓰는 경우를 방지하기 위해서 한다.
반응형
'IT > Python' 카테고리의 다른 글
[자연어처리] 간단하게 만든 긍정, 부정, 중립 분류 using naive bayes classifier (8) | 2014.12.19 |
---|---|
[자연어처리] a little spell-checker using string edit distance (0) | 2014.12.19 |
python beautifulsoap - table parsing (0) | 2014.12.19 |
python txt파일 읽기 에러 'cp949' (4) | 2014.12.19 |
sklearn.cross_validation.train_test_split(*arrays, **options) (0) | 2014.12.19 |