사용자 도구

사이트 도구


python:file

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
python:file [2012/07/17 12:06]
kwon37xi 새로 만듦
python:file [2012/08/01 10:50] (현재)
kwon37xi
줄 1: 줄 1:
 ====== Python File 다루기 ====== ====== Python File 다루기 ======
 +  * [[http://docs.python.org/tutorial/inputoutput.html|Python Input Output]]
   * [[http://docs.python.org/release/2.5.2/lib/bltin-file-objects.html|Builtin File Object]]   * [[http://docs.python.org/release/2.5.2/lib/bltin-file-objects.html|Builtin File Object]]
 +
 +===== 파일열기 =====
 +  * ''open('파일명','모드')'' 함수를 사용한다.
 +  * 모드
 +    * r : 읽기
 +    * w : 쓰기. 기존 내용 지워짐.
 +    * a : 내용 추가 쓰기.
 +    * r+, w+, a+ : 파일 업데이트. w+의 경우 기존내용 삭제
 +    * b : MS Windows에서 바이너리 모드 -> rb, wb, r+b 존재
 +    * 
 +
  
 ===== 파일 열어 줄 읽기 ===== ===== 파일 열어 줄 읽기 =====
줄 22: 줄 33:
 </code> </code>
  
 +===== Unicode File =====
 +==== 읽기 ====
 +codecs 모듈을 사용해서 파일을 열면 내용을 Unicode로 읽어들인다.
 +<code python>
 +import codecs
 +f = codecs.open('파일명', encoding='utf-8')
 +for line in f:
 +    print repr(line)
 +</code>
  
 +==== 쓰기 ====
 +쓰기시에는 unicode 객체를 저장해야 한다.
 +<code python>
 +f = codecs.open('test', encoding='utf-8', mode='w+')
 +f.write(u'\u4500 blah blah blah\n')
 +</code>
python/file.1342494408.txt.gz · 마지막으로 수정됨: 2012/07/17 12:06 저자 kwon37xi