
< 이전 글

.
├── ex_1.py
├── ex_2.py
├── ex_3.py
└── txt_examples
with open('txt_examples/ex_3.txt', 'w') as f:
f.write('This is a test file.\n')
f.write('It contains some text.\n')
f.write('This is the third line.\n')
.
├── ex_1.py
├── ex_2.py
├── ex_3.py
└── txt_examples
└── ex_3.txt
txt_examples/ex_3.txt 경로에 ex_3.txt 파일이 생성됨을 확인할 수 있다.with open('파일경로', mode) 기본 문법mode는 파일을 어떤 용도로 열 것인지 지정하는 옵션이다.
모드 (mode) | 의미 | 파일이 없을 때 | 파일이 있을 때 |
|---|---|---|---|
'r' | 읽기 (read) | 에러 발생 | 내용 읽기 가능 |
'w' | 쓰기 (write) | 새로 생성 | 내용 덮어씀 |
'a' | 추가 (append) | 새로 생성 | 맨 뒤에 덧붙임 |
# 1. 파일 쓰기 예제
# - 기존 내용을 덮어쓰고 새 내용 저장
with open('txt_examples/ex_3.txt', 'w') as f:
f.write('This is a test file.\n')
f.write('It contains some text.\n')
f.write('This is the third line.\n')
# 2. 파일 읽기 예제
# - 파일 전체 내용을 읽어 출력
with open('txt_examples/ex_3.txt', 'r') as f:
content = f.read()
print("File content:")
print(content)
# 3. 파일 추가 예제
# - 기존 파일 끝에 새 줄을 추가
with open('txt_examples/ex_3.txt', 'a') as f:
f.write('This line is added at the end of the file.\n')
# 추가 후 다시 읽어 확인
with open('txt_examples/ex_3.txt', 'r') as f:
content = f.read()
print("File content (with line added):")
print(content)
File content:
This is a test file.
It contains some text.
This is the third line.
File content (with line added):
This is a test file.
It contains some text.
This is the third line.
This line is added at the end of the file.
.
├── csv_examples
├── ex_1.py
├── ex_2.py
├── ex_3.py
├── ex_4.py
└── txt_examples
└── ex_3.txt
import pandas as pd
states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']
population = [39538223, 29145505, 21538187, 20201249, 12812508]
dict_states = {'State': states, 'Population': population}
df_states = pd.DataFrame.from_dict(dict_states)
df_csv = df_states.to_csv('csv_examples/states_population.csv', index=False)
print(df_states)
.
├── csv_examples
│ └── states_population.csv
├── ex_1.py
├── ex_2.py
├── ex_3.py
├── ex_4.py
└── txt_examples
└── ex_3.txt
#Handling exceptions errors: Try-Except
#예를 들어,
#List에 여러 개의 타입 values가 섞여 있을 때
#TypeError가 발생할 수 있음.
example_list = [2,4,6, "california", 8, 10]
for element in example_list:
print(element/2)
1.0
2.0
3.0
Traceback (most recent call last):
File "/Users/choiwoojin/Developer/web-crawling-tutorial/ex_5.py", line 9, in <module>
print(element/2)
~~~~~~~^~
TypeError: unsupported operand type(s) for /: 'str' and 'int'
#Handling exceptions errors: Try-Except
#예를 들어,
#List에 여러 개의 타입 values가 섞여 있을 때
#TypeError가 발생할 수 있음.
example_list = [2,4,6, "california", 8, 10]
# try-except 구문을 사용하여 예외 처리
#try:
# 예외가 발생할 수 있는 코드
#except 예외타입:
# 예외가 발생했을 때 실행할 코드
for element in example_list:
try:
print(element/2)
except:
print(f"Error: {element} is not a number, cannot divide by 2.")
2.0
4.0
6.0
Error: california is not a number, cannot divide by 2.
8.0
10.0