문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
wiki:ai:python:파일_읽기_쓰기 [2020/06/25 15:43] hylee |
wiki:ai:python:파일_읽기_쓰기 [2023/01/13 18:44] (현재) |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| - | ====== | + | ====== |
| <WRAP left notice 80%> | <WRAP left notice 80%> | ||
| - | * description : 모듈과 패키지 | + | * description : 파일 읽기, 쓰기 |
| * author | * author | ||
| * email : hylee@repia.com | * email : hylee@repia.com | ||
| 줄 8: | 줄 8: | ||
| <WRAP clear/> | <WRAP clear/> | ||
| - | ===== 모듈과 패키지 | + | ===== 파일 읽기, 쓰기 |
| - | > 현재 진행중인 프로젝트 안에 참조할 파일을 만든 뒤 진행하시면 됩니다. | + | > 현재 진행중인 프로젝트 안에 참조할 파일을 만든 뒤 진행하시면 됩니다.\\ |
| + | |||
| + | > 파일 쓰기 예제는 콘솔에 써진 파일 내용을 출력해 놨으니 콘솔로 확인하시면 됩니다.\\ | ||
| ==== 준비 사항 ==== | ==== 준비 사항 ==== | ||
| - | === 패키지 | + | === 참조 |
| {{: | {{: | ||
| ==== 파일 내용 ==== | ==== 파일 내용 ==== | ||
| 줄 32: | 줄 34: | ||
| ==== 예제 코드 ==== | ==== 예제 코드 ==== | ||
| <code python> | <code python> | ||
| + | # Section09 | ||
| + | # 파일 읽기, 쓰기 | ||
| + | |||
| + | # 읽기 모드 r, 쓰기 모드(기존 파일 삭제) w, 추가 모드(파일 생성 또는 추가) a | ||
| + | # 기타 : https:// | ||
| + | # 상대 경로(' | ||
| + | |||
| + | # 파일 읽기 | ||
| + | # 예제1 | ||
| + | print("# | ||
| + | print("# | ||
| + | f = open(' | ||
| + | contents = f.read() | ||
| + | print(contents) | ||
| + | # print(dir(f)) | ||
| + | # 반드시 close 리소스 반환 | ||
| + | f.close() | ||
| + | |||
| + | print() | ||
| + | |||
| + | # 예제2 | ||
| + | print("# | ||
| + | # with 는 close()를 사용 안해도 된다. | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(iter(c)) | ||
| + | print(list(c)) | ||
| + | print(c) | ||
| + | |||
| + | print() | ||
| + | |||
| + | # read : 전체 내용 읽기, read(10) : 10글자 읽기 | ||
| + | |||
| + | # 예제3 | ||
| + | print("# | ||
| + | with open(' | ||
| + | for c in f: | ||
| + | # print(c) | ||
| + | print(c.strip()) | ||
| + | |||
| + | print() | ||
| + | |||
| + | # 예제4 | ||
| + | print("# | ||
| + | with open(' | ||
| + | contents = f.read() | ||
| + | print('>', | ||
| + | contents = f.read() | ||
| + | print('>', | ||
| + | f.seek(0, 0) | ||
| + | contents = f.read() | ||
| + | print('>', | ||
| + | |||
| + | # readline : 한 줄씩 읽기, readline(문자수) : 문자수 읽기 | ||
| + | |||
| + | print() | ||
| + | |||
| + | # 예제5 | ||
| + | print("# | ||
| + | with open(' | ||
| + | # 한줄씩 읽어 온다. | ||
| + | line = f.readline() | ||
| + | while line: | ||
| + | print(line, end='' | ||
| + | line = f.readline() | ||
| + | |||
| + | # readlines : 전체 읽은 후 라인 단위 리스트 저장 | ||
| + | |||
| + | print() | ||
| + | print() | ||
| + | |||
| + | # 예제6 | ||
| + | print("# | ||
| + | with open(' | ||
| + | contents = f.readlines() | ||
| + | print(contents) | ||
| + | print() | ||
| + | # list 하나씩 빼오기 | ||
| + | for c in contents: | ||
| + | print(c, end='' | ||
| + | |||
| + | print() | ||
| + | print() | ||
| + | |||
| + | # 예제7 | ||
| + | print("# | ||
| + | with open(' | ||
| + | score = [] | ||
| + | for line in f: | ||
| + | score.append(int(line)) | ||
| + | print(score) | ||
| + | print(' | ||
| + | |||
| + | # 파일 쓰기 | ||
| + | print() | ||
| + | print() | ||
| + | print() | ||
| + | print() | ||
| + | print() | ||
| + | print("# | ||
| + | |||
| + | # 예제1 | ||
| + | print("# | ||
| + | with open(' | ||
| + | f.write(' | ||
| + | | ||
| + | print( | ||
| + | | ||
| + | with open(' | ||
| + | f.write(' | ||
| + | """ | ||
| + | ) | ||
| + | print("# | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(c) | ||
| + | | ||
| + | print() | ||
| + | print() | ||
| + | # 예제2 | ||
| + | print("# | ||
| + | with open(' | ||
| + | f.write(' | ||
| + | print( | ||
| + | r""" | ||
| + | with open(' | ||
| + | f.write(' | ||
| + | """ | ||
| + | ) | ||
| + | print("# | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(c) | ||
| + | |||
| + | |||
| + | print() | ||
| + | print() | ||
| + | # 예제3 | ||
| + | print("# | ||
| + | from random import randint | ||
| + | |||
| + | with open(' | ||
| + | # range(6) -> 0~5 | ||
| + | for cnt in range(6): | ||
| + | # randint(50, 100) -> 50 ~ 100 중 랜덤 한 숫자 | ||
| + | f.write(str(randint(50, | ||
| + | f.write(' | ||
| + | |||
| + | print( | ||
| + | r""" | ||
| + | from random import randint | ||
| + | |||
| + | with open(' | ||
| + | # range(6) -> 0~5 | ||
| + | for cnt in range(6): | ||
| + | # randint(50, 100) -> 50 ~ 100 중 랜덤 한 숫자 | ||
| + | f.write(str(randint(50, | ||
| + | f.write(' | ||
| + | """ | ||
| + | ) | ||
| + | print("# | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(c) | ||
| + | |||
| + | |||
| + | print() | ||
| + | print() | ||
| + | # 예제4 | ||
| + | # writelines : 리스트 -> 파일로 저장 | ||
| + | print("# | ||
| + | with open(' | ||
| + | list = [' | ||
| + | f.writelines(list) | ||
| + | | ||
| + | print( | ||
| + | r""" | ||
| + | with open(' | ||
| + | list = [' | ||
| + | f.writelines(list) | ||
| + | """ | ||
| + | ) | ||
| + | print("# | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(c) | ||
| + | |||
| + | |||
| + | print() | ||
| + | print() | ||
| + | |||
| + | # 예제5 | ||
| + | print("# | ||
| + | with open(' | ||
| + | print(' | ||
| + | print(' | ||
| + | | ||
| + | print( | ||
| + | r""" | ||
| + | with open(' | ||
| + | print(' | ||
| + | print(' | ||
| + | """ | ||
| + | ) | ||
| + | print("# | ||
| + | with open(' | ||
| + | c = f.read() | ||
| + | print(c) | ||
| + | |||
| + | |||
| + | |||
| + | print() | ||
| </ | </ | ||
| 줄 38: | 줄 252: | ||
| ==== 실행 콘솔 ==== | ==== 실행 콘솔 ==== | ||
| <code console> | <code console> | ||
| + | #==== 파일 읽기 ==== | ||
| + | #=== 파일 읽기 1 === | ||
| + | The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 2 - with === | ||
| + | < | ||
| + | [' | ||
| + | ' | ||
| + | ' | ||
| + | ',', | ||
| + | ' | ||
| + | The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 3 - strip() === | ||
| + | The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 4 - 커서위치 === | ||
| + | > The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | > | ||
| + | > The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 5 - readline() 한줄씩 === | ||
| + | The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 6 - readlines() 리스트형태 === | ||
| + | ['The film, projected in the form of animation, | ||
| + | dialogues, | ||
| + | |||
| + | The film, projected in the form of animation, | ||
| + | imparts the lesson of how wars can be eluded through reasoning and peaceful dialogues, | ||
| + | which eventually paves the path for gaining a fresh perspective on an age-old problem. | ||
| + | The story also happens to centre around two parallel characters, Shundi King and Hundi King, | ||
| + | who are twins, but they constantly fight over unresolved issues planted in their minds | ||
| + | by external forces from within their very own units. | ||
| + | |||
| + | #=== 파일 읽기 7 - append() 읽어서 추가 === | ||
| + | [95, 78, 92, 89, 100, 66] | ||
| + | Average : 86.667 | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | #==== 파일 쓰기 ==== | ||
| + | #=== 파일 쓰기 w === | ||
| + | |||
| + | with open(' | ||
| + | f.write(' | ||
| + | |||
| + | #=== ./ | ||
| + | niceman! | ||
| + | |||
| + | |||
| + | #=== 파일 쓰기 a === | ||
| + | |||
| + | with open(' | ||
| + | f.write(' | ||
| + | |||
| + | #=== ./ | ||
| + | niceman!niceman!! | ||
| + | |||
| + | |||
| + | #=== 파일 쓰기 random w === | ||
| + | |||
| + | from random import randint | ||
| + | |||
| + | with open(' | ||
| + | # range(6) -> 0~5 | ||
| + | for cnt in range(6): | ||
| + | # randint(50, 100) -> 50 ~ 100 중 랜덤 한 숫자 | ||
| + | f.write(str(randint(50, | ||
| + | f.write(' | ||
| + | |||
| + | #=== ./ | ||
| + | 83 | ||
| + | 68 | ||
| + | 50 | ||
| + | 73 | ||
| + | 60 | ||
| + | 71 | ||
| + | |||
| + | |||
| + | |||
| + | #=== 파일 쓰기 writelines 리스트 -> 파일로 저장 === | ||
| + | |||
| + | with open(' | ||
| + | list = [' | ||
| + | f.writelines(list) | ||
| + | |||
| + | #=== ./ | ||
| + | Kim | ||
| + | Park | ||
| + | Lee | ||
| + | |||
| + | |||
| + | |||
| + | #=== 파일 쓰기 print() 를 파일에 씀 === | ||
| + | |||
| + | with open(' | ||
| + | print(' | ||
| + | print(' | ||
| + | |||
| + | #=== ./ | ||
| + | Test Contents! | ||
| + | Test Contents!! | ||
| + | |||
| </ | </ | ||
| 줄 44: | 줄 394: | ||
| - | {{tag> | + | {{tag> |