Repository navigation
Expand file tree
/
Copy pathFileIO.py
More file actions
103 lines (85 loc) · 2.99 KB
/
Copy pathFileIO.py
File metadata and controls
103 lines (85 loc) · 2.99 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# =============================================
# 파일 입출력 예제
# =============================================
import os
import csv
# 테스트용 디렉토리 생성
test_dir = os.path.dirname(os.path.abspath(__file__))
if not os.path.exists(test_dir):
test_dir = "."
# ==================== 파일 쓰기 (w 모드) ====================
print("=== 파일 쓰기 ===")
file_path = os.path.join(test_dir, "test_file.txt")
with open(file_path, "w", encoding="utf-8") as f:
for i in range(1, 6):
f.write(f"{i} Line\n")
print("파일 쓰기 완료")
# ==================== 파일 읽기 (r 모드) ====================
print("\n=== 파일 읽기 - read() ===")
with open(file_path, "r", encoding="utf-8") as f:
data = f.read()
print(data)
print("=== 파일 읽기 - readline() ===")
with open(file_path, "r", encoding="utf-8") as f:
line = f.readline()
print(line, end="")
line = f.readline()
print(line, end="")
print("\n=== 파일 읽기 - while ===")
with open(file_path, "r", encoding="utf-8") as f:
while True:
line = f.readline()
if not line:
break
print(line, end="")
# ==================== 파일 추가 (a 모드) ====================
print("\n\n=== 파일 추가 ===")
with open(file_path, "a", encoding="utf-8") as f:
for i in range(6, 11):
f.write(f"{i} Line append\n")
print("파일 추가 완료")
# 확인
with open(file_path, "r", encoding="utf-8") as f:
print(f.read())
# ==================== CSV 파일 ====================
print("=== CSV 파일 쓰기 ===")
csv_path = os.path.join(test_dir, "data.csv")
data = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "London"],
["Charlie", 35, "Seoul"]
]
with open(csv_path, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(data)
print("CSV 파일 쓰기 완료")
print("\n=== CSV 파일 읽기 ===")
with open(csv_path, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
print(row)
# ==================== TSV 파일 ====================
print("\n=== TSV 파일 쓰기 ===")
tsv_path = os.path.join(test_dir, "data.tsv")
with open(tsv_path, "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f, delimiter="\t")
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Alice", 25, "New York"])
writer.writerow(["Bob", 30, "London"])
print("TSV 파일 쓰기 완료")
# ==================== 디렉토리 작업 ====================
print("\n=== 디렉토리 작업 ===")
new_dir = os.path.join(test_dir, "output")
if not os.path.exists(new_dir):
os.mkdir(new_dir)
print("디렉토리 생성 완료")
else:
print("디렉토리 이미 존재")
# 디렉토리 내 파일 작성
output_file = os.path.join(new_dir, "output.txt")
with open(output_file, "w", encoding="utf-8") as f:
for i in range(1, 11):
f.write(f"{i} 번째 줄입니다.\n")
print("output.txt 파일 생성 완료")
print("\n파일 입출력 예제 완료!")