-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.py
More file actions
25 lines (19 loc) · 1.36 KB
/
file.py
File metadata and controls
25 lines (19 loc) · 1.36 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
file = open('hello.txt', 'w') # hello.txt 파일을 쓰기 모드(w)로 열기. 파일 객체 반환
file.write('Hello, world!') # 파일에 문자열 저장
file.close() # 파일 객체 닫기
file = open('hello.txt', 'r') # hello.txt 파일을 읽기 모드(r)로 열기. 파일 객체 반환
s = file.read() # 파일에서 문자열 읽기
print(s) # Hello, world!
file.close() # 파일 객체 닫기
# 자동으로 파일 객체 닫기
with open('hello.txt', 'r') as file: # hello.txt 파일을 읽기 모드(r)로 열기
s = file.read() # 파일에서 문자열 읽기
print(s) # Hello, world!
# for 반복문으로 파일의 내용을 줄 단위로 읽기
with open('hello.txt', 'r') as file: # hello.txt 파일을 읽기 모드(r)로 열기
for line in file: # for에 파일 객체를 지정하면 파일의 내용을 한 줄씩 읽어서 변수에 저장함
print(line.strip('\n')) # 파일에서 읽어온 문자열에서 \n 삭제하여 출력
# 반복문으로 N-gram 출력하기
text = 'Hello'
for i in range(len(text) - 1): # 2-gram이므로 문자열의 끝에서 한 글자 앞까지만 반복함
print(text[i], text[i + 1], sep='') # 현재 문자와 그다음 문자 출력