Python/ETC

[python] 파이썬 파일 함수 (File Methods)

꼰대 2021. 5. 18. 17:27

1. close()

- open된 파일을 닫음

- 만약 닫지 않는다면 다른 프로그램에서 해당 파일을 변경할 수 없음

- 참고 : https://www.w3schools.com/python/ref_file_close.asp

>> f = open('test.txt', 'r', encoding='utf8')

>> print(f.read())

>> f.close()

This is test file.
https://ggondae.tistory.com
한글도 잘 될거에요

 

- with문을 사용하여 파일을 open할 경우 파일은 구문 내에서만 유효

>> with open('test.txt', 'r', encoding='utf8') as f:

...      print(f.read())


2. fileno()

- 정수형 파일 기술자(File descriptor) 값을 반환

- 참고 : https://www.w3schools.com/python/ref_file_fileno.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.fileno()

...      print(x)

3


3. flush()

- 내부 버퍼을 비움 (파일 전송)

- 참고 : https://www.w3schools.com/python/ref_file_flush.asp

>> with open('write_test.txt', 'a', encoding='utf8') as f:

...     f.write('line1 text')

...     f.flush()

...     f.write('\nline2 text')


4. read(size)

- 파일을 읽어 반환

- 인자값이 있을 경우 인자값의 바이트 크기만큼 읽어 반환, 기본값은 -1로 이는 전체를 의미

- 참고 : https://www.w3schools.com/python/ref_file_read.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.read()

...      print(x)

This is test file.
https://ggondae.tistory.com
한글도 잘 될거에요

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.read(10)

...      print(x)

This is te


5. readable()

- 파일이 read()가 가능한 파일이라면 True, 아니면 False 반환

- 참고 : https://www.w3schools.com/python/ref_file_readable.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readable()

...      print(x)

True


6. readline(size)

- 파일에서 한 라인을 읽어 반환 (개행 포함)

- 인자값이 있을 경우 인자값의 바이트 크기만큼 읽어 반환, 기본값은 -1로 이는 전체를 의미

- 참고 : https://www.w3schools.com/python/ref_file_readline.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readline()

...      print(x)

This is test file.
(개행 공백)

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readline()

...      y = f.readline()

...      print(x)

...      print(y)

This is test file.
(개행 공백)

https://ggondae.tistory.com

(개행 공백)


7. readlines(size)

- 파일에서 모든 라인을 읽어 라인 당 리스트로 저장하여 반환

- 인자값이 있을 경우 인자값이 바이트 수 (\n 제외)가 포함된 리스트를 반환

- 참고 : https://www.w3schools.com/python/ref_file_readlines.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readlines()

...      print(x)

['This is test file.\n', 'https://ggondae.tistory.com\n', '한글도 잘 될거에요']

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readlines(18)

...      print(x)

['This is test file.\n']

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.readlines(19)

...      print(x)

['This is test file.\n', 'https://ggondae.tistory.com\n']


8. seek(offset)

- 파일의 새로운 시작 지점을 인자로 받아 저장

- 참고 : https://www.w3schools.com/python/ref_file_seek.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      f.seek(4)

...      x = f.read()

...      print(x)

 is test file.
https://ggondae.tistory.com
한글도 잘 될거에요

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...      f.seek(19)

...      x = f.read()

...      print(x)

(개행 공백)
https://ggondae.tistory.com
한글도 잘 될거에요


9. seekable()

- 파일이 seek()가 가능하다면 True, 아니면 False 반환

- 참고 : https://www.w3schools.com/python/ref_file_seekable.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.seekable()

...      print(x)

True


10. tell()

- 현재 파일의 시작점 위치 정보 반환

- 참고 : https://www.w3schools.com/python/ref_file_tell.asp

>> with open('test.txt', 'r', encoding='utf8') as f:

...      x = f.tell()

...      f.seek(19)

...      y = f.tell()

...      print(x)

...      print(y)

0

19


11. truncate(size)

- 파일의 크기를 인자로 받은 값으로 변경 (a 옵션으로 파일 open)

- open한 파일에 내용이 있다면 인자로 받은 사이즈 이외의 값은 삭제

- 인자가 없다면 현재 파일 크기 변동 없음

- 참고 : https://www.w3schools.com/python/ref_file_truncate.asp

>> with open('test.txt', 'a', encoding='utf8') as f:

...     f.truncate(18)

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...     x = f.read()

...     print(x)

This is test file.


12. writable()

- 파일이 쓰기 가능 상태이면 True, 아니면 False 반환

- 파일 open 시 'a' 혹은 'w' 옵션으로 오픈해야 쓰기 가능

>> with open('test.txt', 'a', encoding='utf8') as f:

...     x = f.writable()

...     print(x)

True

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...     x = f.writable()

...     print(x)

False


13. write(text or byte)

- 인자로 받은 값을 파일에 쓰기

- 참고 : https://www.w3schools.com/python/ref_file_write.asp

>> with open('test.txt', 'a', encoding='utf8') as f:

...     f.write('\n추가되는 구문입니다.')

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...     x = f.read()

...     print(x)

This is test file.
https://ggondae.tistory.com
한글도 잘 될거에요
추가되는 구문입니다.

 

>> with open('test.txt', 'w', encoding='utf8') as f:

...     f.write('w옵션 구문입니다.')

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...     x = f.read()

...     print(x)

w옵션 구문입니다.

 

* 파일 open 시 쓰기 옵션

w : 파일 내 데이터를 모두 지우고 write()를 통해 입력 받은 데이터 저장

a : 파일 내 index 맨 마지막부터 write()를 통해 입력 받은 데이터 저장


14. writelines(iterable)

- 반복 가능한 object를 인자로 받아 한 라인에 모두 입력

- 참고 : https://www.w3schools.com/python/ref_file_writelines.asp

>> list = ['This is Test', '\ntest files']

>> with open('test.txt', 'w', encoding='utf8') as f:

...     f.writelines(list)

 

>> with open('test.txt', 'r', encoding='utf8') as f:

...     print(f.read())

This is Test
test files

반응형