Read & write
๐ ํ์ผ ์ฝ๊ธฐ ๋ฐ ์ฐ๊ธฐ
๐ ์ฝ๊ธฐ ๋ชจ๋ : r, ์ฐ๊ธฐ ๋ชจ๋ : w, ์ถ๊ฐ ๋ชจ๋ : a, ํ
์คํธ ๋ชจ๋ : t, ๋ฐ์ด๋๋ฆฌ ๋ชจ๋ : b
๐ก Code
f = open('./resource/it_news.txt','r',encoding = 'UTF-8')
print(dir(f))
print(f.encoding)
print(f.name)
print(f.mode)
cts=f.read()
print(cts)
f.close()
with open('./resource/it_news.txt','r',encoding = 'UTF-8') as f:
c = f.read()
print(c)
print(iter(c))
print(list(c))
print()
with open('./resource/it_news.txt','r',encoding = 'UTF-8') as f:
c = f.read(20)
print(c)
c = f.read(20)
print(c)
f.seek(0,0)
c = f.read(20)
print(c)
print()
with open('./resource/it_news.txt','r',encoding = 'UTF-8') as f:
line = f.readline()
print(line)
line = f.readline()
print(line)
print()
with open('./resource/it_news.txt','r',encoding = 'UTF-8') as f:
cts = f.readlines()
print(cts)
print()
for c in cts:
print(c,end='')
print()
with open('./resource/contents1.txt','w',encoding = 'UTF-8') as f:
f.write('I love python\n')
with open('./resource/contents1.txt','a',encoding = 'UTF-8') as f:
f.write('I love python2\n')
with open('./resource/contents2.txt','w') as f:
list = ['Orange\n','Apple\n','Banana\n','Melon\n']
f.writelines(list)
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)