본문 바로가기

개발/Python

[Python] 해당 타입의 자료에 들어있는 메소드 알아내기

int면 int, string이면 string 같은 타입은 이미 내장되어 있는 메소드가 있다

그게 어떤 것들이 있는지 메소드의 리스트를 출력하는 방법이다.

file=open("textfile.txt", 'r')
print(type(file))
print(dir(file))

Out

<class '_io.TextIOWrapper'>

['_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']

이렇게 출력된다

 

 

[Python] 파일 열기, 읽기

open('파일명입력', '모드 선택') file = open('hello.txt', 'r') 모드 'w'는 파일을 작성할 때 'r'은 파일을 읽을 때 Methods .read() : 내용을 하나의 문자열로 만들어 내보낸다 texts=file.read()​ Reference..

chae52.tistory.com

에서 소개한 read 메소드도 여기에 들어있는 것을 확인할 수 있다