{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 13. 파일 입출력\n", "***\n", "***\n", "- 파일을 열어서 읽고, 쓰고, 덧붙이는 방법\n", " - open(filename, mode) 내장 함수로 filename 이름을 지닌 file 객체를 얻는다. \n", " - 얻어진 파일 객체에서 자료를 읽거나, 쓰거나, 덧붙이는 작업 수행\n", " - 모든 작업이 끝나면 close()를 호출하여 작업 프로세스의 자원 점유 해제" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 1 파일 입출력 방법\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-1 파일 처리 모드의 종류\n", "- open 내장 함수의 두번째 인자 mode 설명\n", " - 두번째 인자 mode 생략시에는 읽기 전용(r) 모드로 설정\n", " \n", "| Mode | 간단 설명 | 자세한 설명\n", "|--------|-----------------------------|------------|\n", "| 'r' | 읽기 전용 (기본 모드) | 파일 객체를 읽기 모드로 생성하고, 파일 포인터를 파일 처음 위치에 놓는다.|\n", "| 'w' | 쓰기 전용 | 새로운 파일을 쓰기 모드로 생성하거나 해당 파일이 이미 존재하면 내용을 모두 없에면서 쓰기 모드로 생성하고, 파일 포인터를 파일 처음 위치에 놓는다. |\n", "| 'a' | 파일 끝에 추가 | 이미 존재하는 파일을 쓰기 모드로 생성하거나 파일이 존재하지 않으면 새롭게 파일을 생성하면서 쓰기 모드로 생성하고, 파일 포인터를 파일의 마지막 위치에 놓는다. 따라서, 이후 작성되는 내용은 파일의 뒷 부분에 추가된다.|\n", "\n", " - 이진 파일로 저장을 위해서는 아래 모드 사용\n", "\n", "| Mode | 간단 설명 |\n", "|--------|-----------------------------|\n", "| 'rb' | 이진 파일 읽기 전용 |\n", "| 'wb' | 이진 파일 쓰기 전용 |\n", "| 'ab' | 이진 파일 끝에 추가 |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-2 파일 쓰기" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/jubong/git/python-link-e-learning/python3.6\n" ] } ], "source": [ "import os\n", "\n", "print(os.getcwd())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "s = \"\"\"Its power: Python developers typically report \n", "they are able to develop applications in a half\n", "to a tenth the amount of time it takes them to do\n", "the same work in such languages as C.\"\"\"\n", "f = open('t.txt', 'w')\n", "f.write(s) # 문자열을 파일에 기록\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-3 파일 읽기\n", "- read() 메소드 사용\n", " - 대용량 파일인 경우 사용 비추천" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Its power: Python developers typically report \n", "they are able to develop applications in a half\n", "to a tenth the amount of time it takes them to do\n", "the same work in such languages as C.\n", "\n" ] } ], "source": [ "#f = file('t.txt') \n", "f = open('t.txt', 'r')\n", "s = f.read()\n", "print(s)\n", "print(type(s))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- close()을 마지막에 호출하지 않으면 해당 file 객체가 다른 값으로 치환되거나 프로그램이 종료될 때 자동으로 close()가 불리워진다.\n", " - 하지만 명시적으로 close()를 호출하는 것을 권장함" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- with ~ as ~ 사용 추천\n", " - with ~ as ~ 블럭이 끝나면 자동으로 close()를 해줌 " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "s = \"\"\"Its power: Python developers typically report \n", "they are able to develop applications in a half\n", "to a tenth the amount of time it takes them to do\n", "the same work in such languages as C.\"\"\"\n", "with open('t.txt', 'w') as f:\n", " f.write(s) # 문자열을 파일에 기록" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Its power: Python developers typically report \n", "they are able to develop applications in a half\n", "to a tenth the amount of time it takes them to do\n", "the same work in such languages as C.\n", "\n" ] } ], "source": [ "with open('t.txt', 'r') as f:\n", " s = f.read()\n", " print(s)\n", " print(type(s))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-4 라인 단위로 파일 읽기\n", "- 총 4가지 방법 존재\n", " - 파일 객체의 반복자(iterator) 이용하기\n", " - 파일 객체의 반복자는 각 라인별로 내용을 읽어오도록 설정되어 있음\n", " - 파일을 라인별로 읽는 방법 중 가장 효과적임\n", " - readline(): 한번에 한줄씩 읽는다.\n", " - readlines(): 파일 전체를 라인 단위로 끊어서 리스트에 저장한다.\n", " - xreadlines(): python3.6에서는 지원하지 않음 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 파일 객체의 반복자 사용" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 : Its power: Python developers typically report \n", "2 : they are able to develop applications in a half\n", "3 : to a tenth the amount of time it takes them to do\n", "4 : the same work in such languages as C." ] } ], "source": [ "f = open('t.txt')\n", "\n", "for i, line in enumerate(f):\n", " print(i+1, \":\", line, end=\"\")\n", "\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- readline() 사용" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "1 : Its power: Python developers typically report \n", "2 : they are able to develop applications in a half\n", "3 : to a tenth the amount of time it takes them to do\n", "4 : the same work in such languages as C." ] } ], "source": [ "f = open('t.txt')\n", "line = f.readline()\n", "print(type(line))\n", "i = 1\n", "while line:\n", " print(i, \":\", line, end=\"\")\n", " line = f.readline()\n", " i += 1\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- readlines() 사용\n", " - 각 라인을 모두 읽어서 메모리에 리스트로 저장함\n", " - 대용량 파일인 경우 사용 비추천" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Its power: Python developers typically report \\n', 'they are able to develop applications in a half\\n', 'to a tenth the amount of time it takes them to do\\n', 'the same work in such languages as C.']\n", "\n", "\n", "1 : Its power: Python developers typically report \n", "2 : they are able to develop applications in a half\n", "3 : to a tenth the amount of time it takes them to do\n", "4 : the same work in such languages as C." ] } ], "source": [ "f = open('t.txt')\n", "print(f.readlines())\n", "print(type(f.readlines()))\n", "print()\n", "\n", "f.seek(0)\n", "i = 1\n", "\n", "for line in f.readlines():\n", " print(i, \":\", line, end=\"\")\n", " i += 1\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Its power: Python developers typically report \\n', 'they are able to develop applications in a half\\n', 'to a tenth the amount of time it takes them to do\\n', 'the same work in such languages as C.']\n", "\n", "\n", "1 : Its power: Python developers typically report \n", "2 : they are able to develop applications in a half\n", "3 : to a tenth the amount of time it takes them to do\n", "4 : the same work in such languages as C." ] } ], "source": [ "f = open('t.txt')\n", "print(f.readlines())\n", "print(type(f.readlines()))\n", "print()\n", "f.close()\n", "\n", "f = open('t.txt')\n", "i = 1\n", "for line in f.readlines():\n", " print(i, \":\", line, end=\"\")\n", " i += 1\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- xreadlines() 사용 (python3에서는 지원하지 않는 메소드)\n", " - python3에서는 xreadlines()사용과 파일 객체 f의 반복자 사용하는 경우와 동일함.\n", " " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'_io.TextIOWrapper' object has no attribute 'xreadlines'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m't.txt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mxreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mseek\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: '_io.TextIOWrapper' object has no attribute 'xreadlines'" ] } ], "source": [ "f = open('t.txt')\n", "print(f.xreadlines())\n", "print()\n", "\n", "f.seek(0)\n", "i = 1\n", "for line in f.xreadlines():\n", " print(i, \":\", line,)\n", " i += 1\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-5 라인 단위로 파일 쓰기" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- writelines(): 리스트 안에 있는 각 문자열을 연속해서 파일로 출력한다." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first line\n", "second line\n", "third line\n", "\n" ] } ], "source": [ "lines = ['first line\\n', 'second line\\n', 'third line\\n']\n", "f = open('t1.txt', 'w')\n", "f.writelines(lines)\n", "f.close()\n", "\n", "f = open('t1.txt')\n", "print(f.read())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- write() 이용하여 여러 문자열을 각 라인별로 파일로 출력하는 방법" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first line\n", "second line\n", "third line\n" ] } ], "source": [ "lines = ['first line', 'second line', 'third line']\n", "f = open('t1.txt', 'w')\n", "f.write('\\n'.join(lines))\n", "f.close()\n", "\n", "f = open('t1.txt')\n", "print(f.read())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 텍스트 파일 t.txt의 단어(공백으로 분리된 문자열) 수를 출력하는 방법" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Its power: Python developers typically report \n", "they are able to develop applications in a half\n", "to a tenth the amount of time it takes them to do\n", "the same work in such languages as C.\n", "35\n" ] } ], "source": [ "f = open('t.txt')\n", "s = f.read()\n", "print(s)\n", "\n", "n = len(s.split())\n", "print(n)\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-6 기존 파일에 내용 추가" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "f = open('removeme.txt', 'w') # 파일의 생성\n", "f.write('first line\\n')\n", "f.write('second line\\n')\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first line\n", "second line\n", "third line\n", "\n" ] } ], "source": [ "f = open('removeme.txt', 'a') # 파일 추가 모드로 오픈\n", "f.write('third line\\n')\n", "f.close()\n", "\n", "f = open('removeme.txt') # 파일 읽기\n", "print(f.read())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-7 파일 내 임의 위치로 접근\n", "- 파일 포인터 (pointer)\n", " - 파일 내에서 현재 위치를 가리키고 있음\n", "- 파일 접근 방법\n", " - 순차 접근 (기본 방식): 파일을 앞에서 부터 순차적으로 읽고 쓰는 방식\n", " - 임의 접근: 파일 내 임의 위치에서 읽고 쓰는 방식\n", " - 임의 접근을 위한 file 객체 포인터 (pointer) 관련 메소드\n", " - seek(n): 파일의 가장 첫번째 위치에서 n번째 바이트로 포인터 이동\n", " - tell(): 파일 내 현재 포인터 위치를 반환" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5\n", "6\n", "\n" ] } ], "source": [ "name = 't.txt' \n", "f = open(name, 'w+') # 읽고 쓰기로 오픈, 단, 파일이 이미 존재한다면 기존 파일은 없어지고 다시 생성된다.\n", "s = '0123456789abcdef'\n", "f.write(s)\n", "\n", "f.seek(5) # 시작부터 5바이트 포인터 이동\n", "print(f.tell()) # 현재 위치 돌려줌\n", "print(f.read(1)) # 1문자 읽기\n", "print(f.tell())\n", "print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 2 표준 출력 방향 전환\n", "***\n", "\n", "- sys 모듈의 표준 입출력 관련 객체\n", " - sys.stdout: 표준 출력\n", " - sys.stderr: 표준 에러 출력\n", " - sys.stdin: 표준 입력\n", "- 예를 들어, sys.stdout을 파일 객체로 변환하면 모든 표준 출력(print 출력)은 해당 파일로 저장된다." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-1 표준 출력을 파일로 저장하기" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "!!!!!!" ] } ], "source": [ "import sys\n", "\n", "f = open('t.txt', 'w')\n", "stdout = sys.stdout # 표준 출력 저장해 두기\n", "sys.stdout = f # 파일 객체로 표준 출력 변경\n", "\n", "stdout.write(\"!!!!!!\")\n", "print('Sample output')\n", "print('Good')\n", "print('Good')\n", "f.close()\n", "sys.stdout = stdout # 표준 출력 원상 복구" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sample output\n", "Good\n", "Good\n", "\n" ] } ], "source": [ "f = open('t.txt')\n", "print(f.read())" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "print(\"Hello koreatech!\", file=open(\"output.txt\", \"w\"))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello koreatech!\n", "\n" ] } ], "source": [ "f = open('output.txt')\n", "print(f.read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- print를 직접 이용하여 출력을 다른 객체로 전환하기 \n", " - 아래와 같은 방식은 python3에서 더이상 지원하지 않음" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for >>: 'builtin_function_or_method' and 'OutStream'. Did you mean \"print(, file=)\"?", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m>>\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstderr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Warning, action field not supplied\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'OutStream'. Did you mean \"print(, file=)\"?" ] } ], "source": [ "print >> sys.stderr, \"Warning, action field not supplied\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 대신 아래 방법 사용" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning, action field not supplied\n" ] } ], "source": [ "print(\"Warning, action field not supplied\", file=sys.stderr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 동일 방법으로 표준 출력(print)을 파일 객체로 전환" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "spam string\n", "\n" ] } ], "source": [ "f = open('t.txt', 'w')\n", "print('spam string', file=f)\n", "f.close()\n", "\n", "f = open('t.txt')\n", "print(f.read())\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2-2 StringIO 모듈 사용하기\n", "- StringIO 모듈의 StringIO 클래스 객체\n", " - 파일 객체처럼 입출력 가능한 문자열 객체\n", " - StringIO에 지원되는 메소드는 파일 객체가 지원하는 메소드와 거의 동일하다.\n", " - getvalue() 메소드\n", " - 현재까지 담아 놓은 전체 내용을 반환한다." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 표준 출력으로 문자열 객체에 내용 작성하기\n", " - python3 에서는 io 모듈내에 StringIO 클래스 존재" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "abcdef\n" ] } ], "source": [ "import sys\n", "from io import StringIO\n", "\n", "f = StringIO()\n", "f.write(\"abc\")\n", "f.write(\"def\")\n", "q = f.read()\n", "print(q)\n", "\n", "q1 = f.getvalue()\n", "print(q1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cdef\n" ] } ], "source": [ "f.seek(2)\n", "print(f.read())\n", "\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "def f1():\n", " s = \"\"\n", " for i in range(10000, 1000000):\n", " s += str(i)\n", "\n", "def f2():\n", " s = StringIO()\n", " for i in range(10000, 1000000):\n", " s.write(str(i))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "300 ms ± 9.37 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit f1()" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "262 ms ± 14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit f2()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "import sys\n", "from io import StringIO\n", "\n", "stdout = sys.stdout # 표준 출력 저장해 두기\n", "sys.stdout = f = StringIO() \n", "\n", "print(type(f))\n", "print('Sample output')\n", "print('Good')\n", "print('Good')\n", "\n", "sys.stdout = stdout" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done-------\n", "\n", "Sample output\n", "Good\n", "Good\n", "\n" ] } ], "source": [ "s = f.getvalue()\n", "\n", "print('Done-------')\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 3 파일로의 지속 모듈\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 지속성(Persistence)\n", " - 프로그램 내에 생성된 각종 객체들을 해당 프로그램 종료 이후에도 존재하게 만들고, 그것들을 동일한 또는 다른 프로그램에서 사용하는 기능\n", "- 지속성 기능을 지원하는 모듈\n", " - DBM 관련 모듈\n", " - anydbm, dbm, gdbm, dbhash, dumbdbm\n", " - anydbm: 시스템에서 사용가능한 모듈 중 가장 최적의 모듈을 반환함\n", " - 기본적으로 dumbdbm을 반환한다\n", " - 사전 자료형을 사용하는 것과 동일한 방법으로 사용\n", " - **pickle** 모듈\n", " - 파이썬의 객체를 저장하는 일반화된 지속성 모듈\n", " - 파이썬의 기본 객체뿐만 아니라 사용자 정의의 복잡한 객체도 저장 가능\n", " - 기본적으로 텍스트 모드로 저장하지만 이진 모드로도 저장 가능" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 피클링(pickling) 모듈 사용하기\n", " - file을 open 할 때 이진 파일로 open 하기\n", " - 즉, 모드로서 'wb', 'rb' 적용" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'tom': 4358382, 'jack': 9465215, 'jim': 6851325, 'Joseph': 6584321}\n", "['string', 1234, 0.2345]\n" ] } ], "source": [ "import pickle\n", "\n", "phone = {'tom':4358382, 'jack':9465215, 'jim':6851325, 'Joseph':6584321}\n", "List = ['string', 1234, 0.2345]\n", "Tuple = (phone, List) # 리스트, 튜플, 사전의 복합 객체\n", "\n", "with open('pickle.txt', 'wb') as f: # 파일 객체를 얻는다.\n", " pickle.dump(Tuple, f) # 파일로 출력(pickling), 복합 객체 출력 \n", "\n", "with open('pickle.txt', 'rb') as f: \n", " x,y = pickle.load(f) # 파일에서 읽어오기. 튜플의 내용을 x, y에 받는다.\n", "\n", "print(x) # 사전\n", "print(y) # 리스트" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "import pickle\n", "\n", "class Simple: # 가장 단순한 클래스를 정의\n", " pass\n", "\n", "s = Simple() # 인스턴스 객체 생성\n", "s.count = 10 # 인스턴스 이름 공간에 변수 생성\n", "\n", "with open('pickle2.txt', 'wb') as f:\n", " pickle.dump(s, f) # 인스턴스 저장\n", "\n", "with open('pickle2.txt', 'rb') as f:\n", " t = pickle.load(f) # 인스턴스 가져오기\n", "print(t.count)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### [참고] 다양한 파일 처리 모드\n", "\n", "- open 내장 함수의 두번째 인자 mode 설명\n", " - 두번째 인자 mode 생략시에는 읽기 전용(r) 모드로 설정\n", " \n", "| Mode | 간단 설명 | 자세한 설명\n", "|--------|-----------------------------|------------|\n", "| 'r' | 읽기 전용(기본 모드) | 파일 객체를 읽기 모드로 생성하고, 파일 포인터를 파일 처음 위치에 놓는다.|\n", "| 'w' | 쓰기 전용(기존 파일 내용 삭제) | 파일이 존재하지 않으면 새로운 파일을 쓰기 모드로 생성하고, 해당 파일이 이미 존재하면 내용을 모두 없에면서 쓰기 모드로 생성하고, 파일 포인터를 파일 처음 위치에 놓는다. |\n", "| 'a' | 파일 끝에 추가(쓰기 전용) | 파일이 존재하지 않으면 새롭게 파일을 생성하면서 쓰기 모드로 생성하고, 해당 파일이 이미 존재하면 파일 객체을 쓰기 모드로 생성하면서 파일 포인터를 파일의 마지막 위치에 놓는다. 따라서, 이후 작성되는 내용은 파일의 뒷 부분에 추가됨.|\n", "| 'r+' | 읽고 쓰기 | 파일 객체를 읽고 쓸 수 있도록 생성한다. 파일 포인터를 파일 처음 위치에 놓는다. |\n", "| 'w+' | 읽고 쓰기(기존 파일 내용 삭제) | 파일 객체를 읽고 쓸 수 있도록 생성한다. 파일이 존재하지 않으면 새로운 파일을 생성하고, 해당 파일이 이미 존재하면 내용을 모두 없에면서 생성하고, 파일 포인터를 파일 처음 위치에 놓는다.|\n", "| 'a+' | 읽고 쓰기(파일 끝에 추가) | 파일 객체를 읽고 쓸 수 있도록 생성한다. 파일이 존재하지 않으면 새롭게 파일을 생성하고, 해당 파일이 이미 존재하면 파일 객체을 생성하면서 파일 포인터를 파일의 마지막 위치에 놓는다 (그래서, 이후 작성되는 내용은 파일의 뒷 부분에 추가). |\n", "\n", "\n", " - 이진 파일로 저장을 위해서는 아래 모드 사용\n", "\n", "| Mode | 간단 설명 |\n", "|--------|-----------------------------|\n", "| 'rb' | 이진 파일 읽기 전용 |\n", "| 'wb' | 이진 파일 쓰기 전용(기존 파일 내용 삭제) |\n", "| 'ab' | 이진 파일 끝에 추가(쓰기 전용) |\n", "| 'rb+' | 이진 파일 읽고 쓰기 |\n", "| 'wb+' | 이진 파일 읽고 쓰기(기존 파일 내용 삭제) |\n", "| 'ab+' | 이진 파일 끝에 추가(읽기도 가능) |\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일

" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 1 }