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

(새 문서: ==개념== ;python files *파이썬으로 파일 내용의 읽고 쓰기가 가능 ==파일 열기== *'''open("파일이름", "모드")""" 모드 {| class='wikitable' ! 모드 !!...)
 
잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 3명의 중간 판 13개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개념==
==개념==
;python files
;python files
*파이썬으로 파일 내용의 읽고 쓰기가 가능
*파이썬으로 다른 파일 내용을 읽고 쓰기가 가능


==파일 열기==
==파일 열기==
*'''open("파일이름", "모드")"""
*open("파일이름", "모드")
모드  
'''열기 모드'''
{| class='wikitable'
{| class='wikitable'
! 모드 !! 설명  
! 모드 !! 설명  
18번째 줄: 18번째 줄:
|}
|}


==예시==
==읽기 예시==
;'''open.py''' 파일이 '''hello.txt''' 파일을 열어 그 내용을 출력
;'''open.py''' 파일이 '''hello.txt''' 파일을 열어 그 내용을 출력
*open.py
*open.py
<source lang="python">
<syntaxhighlight lang="python">
file = open('./hello.txt')
file = open("hello.txt", "r")
content = file.read()
content = file.read()
print(content)
print(content)
</syntaxhighlight>
*hello.txt
*hello.txt
<source lang="python">
<syntaxhighlight lang="python">
file = open('./hello.txt')
Hello World!
</syntaxhighlight>
 
==쓰기 예시==
<syntaxhighlight lang="python">
file = open('hello.txt', "w")
file.write("Hello John")
file.close()
 
file = open("./hello.txt", "r")
content = file.read()
content = file.read()
print(content)
print(content)
 
</syntaxhighlight>
</source>
 
[[분류: 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 }}