[Pyton]파일 읽기/쓰기

파이썬에서의 읽기와 쓰기에 대해 알아보자.

# 파일 읽기 및 쓰기
# 읽기모드 : r, 쓰기모드 : w, 추가모드 : a, 텍스트모드(기본값) : t, 바이너리모드 : b

# 파일 읽기(read)
# 예제1
f = open('./resource/it_news.txt', 'r', encoding='UTF-8')
# 속성확인
print(dir(f))
# 인코딩확인
print(f.encoding)
# 파일이름
print(f.name)
# 모드확인
print(f.mode)
# 내용 가져오기
ctx = f.read()
print(ctx)
# 반드시 사용한 리소스를 닫아준다.
f.close()
print()

# 예제2 : with문이 종료되면 파일을 자동으로 닫는다.
with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    c = f.read()
    print(c)
    print(iter(c))
    print(list(c))

print()

# 예제3
# read() : 전체읽기,   read(10) : 10 Byte 가져오기
with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    c = f.read(20) # 20바이트만 가져오고, 커서가 이동됨
    print(c)
    c = f.read(20) # 20바이트만 가져옴, 커서가 이동됨
    print(c)
    c = f.read(20) # 20바이트만 가져옴, 커서가 이동됨
    print(c)
    f.seek(0, 0) # 커서를 처음으로 옮김
    c = f.read(20) # 20바이트만 가져옴 (커서가 이동됨)
    print(c)

print()

# 예제4
# readline() : 한 줄씩 읽어옴
with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    line = f.readline() # 한 줄을 읽어보고 커서를 이동시킴
    print(line)
    line = f.readline() # 한 줄을 읽어보고 커서를 이동시킴
    print(line)
print()

# 예제5
# readlines() : 전체를 읽은 후 줄단위 리스트로 저장
with open('./resource/it_news.txt', 'r', encoding='UTF-8') as f:
    lines = f.readlines()
    print(lines)
    print()
    for line in lines:
        print(line, end='')
print()

# 파일쓰기(write)
# 예제6
with open('./resource/contents1.txt', 'w') as f:
    f.write('I like python\n')

# 예제7
with open('./resource/contents1.txt', 'at') as f:
    f.write('I am jigi\n')

# 예제8
# writelines : 리스트 -> 파일
with open('./resource/contents2.txt', 'w') as f:
    list = ['Orange\n', 'Apple\n', 'Banana\n']
    f.writelines(list)

# 예제9
with open('./resource/contents3.txt', 'w') as f:
    print('Test text write!!', file=f)
    print('Test text write!!', file=f)
    print('Test text write!!', file=f)

결과 :

['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']
UTF-8
./resource/it_news.txt
r
Right now gamers can pay just $1 for access to hundreds of titles across PC 
and Xbox via Microsoft Xbox Game Pass Ultimate service?but dont 
activate that insanely cheap one-month trial just yet. You can lock in up to 
three years of Xbox Game Pass Ultimate with that same dollar if you play 
your cards right.

When you activate Microsoft’s E3 2019 promotion, it not only begins the trial, 
but also converts existing Xbox Live Gold and standard Xbox Game Pass 
subscriptions on your account to Game Pass Ultimate (normally $15 per 
month). Any prepaid time up to the maximum of three years gets the upgrade.



Right now gamers can pay just $1 for access to hundreds of titles across PC 
and Xbox via Microsoft Xbox Game Pass Ultimate service?but dont 
activate that insanely cheap one-month trial just yet. You can lock in up to 
three years of Xbox Game Pass Ultimate with that same dollar if you play 
your cards right.

When you activate Microsoft’s E3 2019 promotion, it not only begins the trial, 
but also converts existing Xbox Live Gold and standard Xbox Game Pass 
subscriptions on your account to Game Pass Ultimate (normally $15 per 
month). Any prepaid time up to the maximum of three years gets the upgrade.


<str_iterator object at 0x0000018C1EA07280>
['R', 'i', 'g', 'h', 't', ' ', 'n', 'o', 'w', ' ', 'g', 'a', 'm', 'e', 'r', 's', ' ', 'c', 'a', 'n', ' ', 'p', 'a', 'y', ' ', 'j', 'u', 's', 't', ' ', '$', '1', ' ', 'f', 'o', 'r', ' ', 'a', 'c', 'c', 'e', 's', 's', ' ', 't', 'o', ' ', 'h', 'u', 'n', 'd', 'r', 'e', 'd', 's', ' ', 'o', 'f', ' ', 't', 'i', 't', 'l', 'e', 's', ' ', 'a', 'c', 'r', 'o', 's', 's', ' ', 'P', 'C', ' ', '\n', 'a', 'n', 'd', ' ', 'X', 'b', 'o', 'x', ' ', 'v', 'i', 'a', ' ', 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', ' ', 'X', 'b', 'o', 'x', ' ', 'G', 'a', 'm', 'e', ' ', 'P', 'a', 's', 's', ' ', 'U', 'l', 't', 'i', 'm', 'a', 't', 'e', ' ', 's', 'e', 'r', 'v', 'i', 'c', 'e', '?', 'b', 'u', 't', ' ', 'd', 'o', 'n', 't', ' ', '\n', 'a', 'c', 't', 'i', 'v', 'a', 't', 'e', ' ', 't', 'h', 'a', 't', ' ', 'i', 'n', 's', 'a', 'n', 'e', 'l', 'y', ' ', 'c', 'h', 'e', 'a', 'p', ' ', 'o', 'n', 'e', '-', 'm', 'o', 'n', 't', 'h', ' ', 't', 'r', 'i', 'a', 'l', ' ', 'j', 'u', 's', 't', ' ', 'y', 'e', 't', '.', ' ', 'Y', 'o', 'u', ' ', 'c', 'a', 'n', ' ', 'l', 'o', 'c', 'k', ' ', 'i', 'n', ' ', 'u', 'p', ' ', 't', 'o', ' ', '\n', 't', 'h', 'r', 'e', 'e', ' ', 'y', 'e', 'a', 'r', 's', ' ', 'o', 'f', ' ', 'X', 'b', 'o', 'x', ' ', 'G', 'a', 'm', 'e', ' ', 'P', 'a', 's', 's', ' ', 'U', 'l', 't', 'i', 'm', 'a', 't', 'e', ' ', 'w', 'i', 't', 'h', ' ', 't', 'h', 'a', 't', ' ', 's', 'a', 'm', 'e', ' ', 'd', 'o', 'l', 'l', 'a', 'r', ' ', 'i', 'f', ' ', 'y', 'o', 'u', ' ', 'p', 'l', 'a', 'y', ' ', '\n', 'y', 'o', 'u', 'r', ' ', 'c', 'a', 'r', 'd', 's', ' ', 'r', 'i', 'g', 'h', 't', '.', '\n', '\n', 'W', 'h', 'e', 'n', ' ', 'y', 'o', 'u', ' ', 'a', 'c', 't', 'i', 'v', 'a', 't', 'e', ' ', 'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', 't', '’', 's', ' ', 'E', '3', ' ', '2', '0', '1', '9', ' ', 'p', 'r', 'o', 'm', 'o', 't', 'i', 'o', 'n', ',', ' ', 'i', 't', ' ', 'n', 'o', 't', ' ', 'o', 'n', 'l', 'y', ' ', 'b', 'e', 'g', 'i', 'n', 's', ' ', 't', 'h', 'e', ' ', 't', 'r', 'i', 'a', 'l', ',', ' ', '\n', 'b', 'u', 't', ' ', 'a', 'l', 's', 'o', ' ', 'c', 'o', 'n', 'v', 'e', 'r', 't', 's', ' ', 'e', 'x', 'i', 's', 't', 'i', 'n', 'g', ' ', 'X', 'b', 'o', 'x', ' ', 'L', 'i', 'v', 'e', ' ', 'G', 'o', 'l', 'd', ' ', 'a', 'n', 'd', ' ', 's', 't', 'a', 'n', 'd', 'a', 'r', 'd', ' ', 'X', 'b', 'o', 'x', ' ', 'G', 'a', 'm', 'e', ' ', 'P', 'a', 's', 's', ' ', '\n', 's', 'u', 'b', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 's', ' ', 'o', 'n', ' ', 'y', 'o', 'u', 'r', ' ', 'a', 'c', 'c', 'o', 'u', 'n', 't', ' ', 't', 'o', ' ', 'G', 'a', 'm', 'e', ' ', 'P', 'a', 's', 's', ' ', 'U', 'l', 't', 'i', 'm', 'a', 't', 'e', ' ', '(', 'n', 'o', 'r', 'm', 'a', 'l', 'l', 'y', ' ', '$', '1', '5', ' ', 'p', 'e', 'r', ' ', '\n', 'm', 'o', 'n', 't', 'h', ')', '.', ' ', 'A', 'n', 'y', ' ', 'p', 'r', 'e', 'p', 'a', 'i', 'd', ' ', 't', 'i', 'm', 'e', ' ', 'u', 'p', ' ', 't', 'o', ' ', 't', 'h', 'e', ' ', 'm', 'a', 'x', 'i', 'm', 'u', 'm', ' ', 'o', 'f', ' ', 't', 'h', 'r', 'e', 'e', ' ', 'y', 'e', 'a', 'r', 's', ' ', 'g', 'e', 't', 's', ' ', 't', 'h', 'e', ' ', 'u', 'p', 'g', 'r', 'a', 'd', 'e', '.', '\n', '\n']

Right now gamers can
 pay just $1 for acc
ess to hundreds of t
Right now gamers can

Right now gamers can pay just $1 for access to hundreds of titles across PC 

and Xbox via Microsoft Xbox Game Pass Ultimate service?but dont 


['Right now gamers can pay just $1 for access to hundreds of titles across PC \n', 'and Xbox via Microsoft Xbox Game Pass Ultimate service?but dont \n', 'activate that insanely cheap one-month trial just yet. You can lock in up to \n', 'three years of Xbox Game Pass Ultimate with that same dollar if you play \n', 'your cards right.\n', '\n', 'When you activate Microsoft’s E3 2019 promotion, it not only begins the trial, \n', 'but also converts existing Xbox Live Gold and standard Xbox Game Pass \n', 'subscriptions on your account to Game Pass Ultimate (normally $15 per \n', 'month). Any prepaid time up to the maximum of three years gets the upgrade.\n', '\n']

Right now gamers can pay just $1 for access to hundreds of titles across PC 
and Xbox via Microsoft Xbox Game Pass Ultimate service?but dont 
activate that insanely cheap one-month trial just yet. You can lock in up to 
three years of Xbox Game Pass Ultimate with that same dollar if you play 
your cards right.

When you activate Microsoft’s E3 2019 promotion, it not only begins the trial, 
but also converts existing Xbox Live Gold and standard Xbox Game Pass 
subscriptions on your account to Game Pass Ultimate (normally $15 per 
month). Any prepaid time up to the maximum of three years gets the upgrade.

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다