반응형

 

open("파일 위치", "r" or "w" 열기모드 , encoding = "파일 형식") 을 통해 입출력을 수행 할 수있습니다

-열기모드

"r"의 경우 파일을 읽을때 사용합니다.

"w"의 경우 파일을 작성할때 사용합니다.

생략할 경우 "r"과 동일합니다

"r+" 읽기/쓰기 겸용모드

"a" 쓰기모드, 기존에 파일이 있으면 이어서 작성. append의 약자

"t" 텍스트 모드, 텍스트 파일을 처리, 기본값

"b" 바이너리 모드, 바이너리 파일(=이진 파일)을 처리

 

# 파일 출력
inFp = None
inStr = ""

inFp = open("D:/python/data.txt", "r", encoding = "utf-8")

inStr = inFp.readline()
print(inStr, end="")

inStr = inFp.readline()
print(inStr, end="")

inStr = inFp.readline()
print(inStr, end="")

inFp.close()
# 파일 입력
outFp = None
outStr = ""

outFp = open("D:/python/data2.txt", "w", encoding ="utf-8")

while True:
  outStr = input("내용 입력 : ")
  if outStr !="":
    outFp.writelines(outStr + "\n")
  else:
    break

outFp.close()
print("--- 정상적으로 파일에 써졌음 ---")
반응형

+ Recent posts