Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 안드로이드
- 스타듀밸리
- IaaS
- objectiveC
- 공급망공격
- 악성앱
- ios
- 드라이브바이다운로드
- 악성스크립트
- 파이썬
- pyqt
- 웹크롤링
- reactjs
- 메타데이터
- 더위쳐
- anaconda
- MacOD
- 네트워크
- 메일헤더
- 정적분석
- 악성코드
- 블록체인
- CPPG
- 준비과정
- paas
- 오늘의게임
- 파일리스
- 프론트앤드
- Python
- 7layer
Archives
- Today
- Total
취보특개
파이썬 파일 처리 관련 코드(python file processing code) 본문
가장 자주쓰면서 쓸 때 마다 기억안나는 파일처리 관련 코드
1. 파일생성(open)
f=open('example.txt','w')
f.close()
2. 파일쓰기(write)
f=open("sample.txt",'w')
f.write("something")
f.write("something" + '\n')
f.close()
3. 파일읽기(readline)
f=open('example.txt','r')
# 한줄 씩 처리가 필요할 때
while True:
line = f.readline()
if not line: break
print(line)
# 한번에 읽어야 할 때
content = f.read()
f.close()
4. 파일 존재여부 확인
import os.path
if os.path.isfile(fname):
print("exist")
5. 파일 특정 줄 삭제
#모든 줄 읽기
with open("yourfile.txt", "r") as f:
lines = f.readlines()
#특정 줄 제외 다시 쓰기
with open("yourfile.txt", "w") as f:
for line in lines:
if line.strip("\n") != "nickname_to_delete":
f.write(line)
'Gaebal > Python' 카테고리의 다른 글
[python] encoding error (0) | 2020.11.27 |
---|---|
[python] Web Crawling Step-By-Step (0) | 2020.11.25 |
PyQT-2 (Designer) (0) | 2020.09.22 |
[python] datetime module (0) | 2020.08.08 |
Anaconda Usage (0) | 2020.05.30 |
Comments