"파이썬 파일입출력"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 2명의 중간 판 3개는 보이지 않습니다)
21번째 줄: 21번째 줄:
;'''open.py''' 파일이 '''hello.txt''' 파일을 열어 그 내용을 출력
;'''open.py''' 파일이 '''hello.txt''' 파일을 열어 그 내용을 출력
*open.py
*open.py
<source lang="python">
<syntaxhighlight lang="python">
file = open("./hello.txt", "r")
file = open("hello.txt", "r")
content = file.read()
content = file.read()
print(content)
print(content)
</source>
</syntaxhighlight>
*hello.txt
*hello.txt
<source lang="python">
<syntaxhighlight lang="python">
Hello World!
Hello World!
</source>
</syntaxhighlight>


==쓰기 예시==
==쓰기 예시==
<source lang="python">
<syntaxhighlight lang="python">
file = open('./hello.txt', "w")
file = open('hello.txt', "w")
file.write("Hello John")
file.write("Hello John")
file.close()
file.close()
40번째 줄: 40번째 줄:
content = file.read()
content = file.read()
print(content)
print(content)
</source>
</syntaxhighlight>
[[분류: python]]
[[분류: python]]

2020년 11월 2일 (월) 02:32 기준 최신판

1 개념[ | ]

python files
  • 파이썬으로 다른 파일 내용을 읽고 쓰기가 가능

2 파일 열기[ | ]

  • open("파일이름", "모드")

열기 모드

모드 설명
r 읽기 모드
w 쓰기 모드
a 추가 모드
b 바이너리 모드

3 읽기 예시[ | ]

open.py 파일이 hello.txt 파일을 열어 그 내용을 출력
  • open.py
file = open("hello.txt", "r")
content = file.read()
print(content)
  • hello.txt
Hello World!

4 쓰기 예시[ | ]

file = open('hello.txt', "w")
file.write("Hello John")
file.close()

file = open("./hello.txt", "r")
content = file.read()
print(content)
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}