IT 이야기/Programming

open (파일관리)

클톡(CloudTalk) 2017. 5. 31. 10:06

>>> f = open('c:\\work\\data.txt','wt')
>>> f.write('한글로 출력')
6
>>> f.write('한글로 출력\n')
7
>>> f.write('abcd\n1234')
9
>>> f.close()
>>> f.closed()
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    f.closed()
TypeError: 'bool' object is not callable
>>> f.closed
True
>>> f = open('c:\\work\\data.txt','rt')
>>> f.read()
'한글로 출력한글로 출력\nabcd\n1234'
>>> f.tell()
34
>>> f.seek(0)
0
>>> f.tell()
0
>>> f.readline()
'한글로 출력한글로 출력\n'
>>> f.readline()
'abcd\n'
>>> f.seek(0)
0
>>> lst = f.readlines()
>>> lst
['한글로 출력한글로 출력\n', 'abcd\n', '1234']
>>> for item in lst:
 print(item)

 
한글로 출력한글로 출력

abcd

1234
>>> for item in lst:
 print(item.replace('\n',''))

 
한글로 출력한글로 출력
abcd
1234

 

(파일안의 내용을 리스트화 시키고 리스트화 시킨 정보에서 \n 을 제외시키는 부분으로 처리하여 제외하여 리스트 표시)