{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# files and dir\n", "dataなどのfileとのやり取りに関するまとめです." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# dirでのやり取り" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## cwd" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/bob/python/doing_math_with_python/symbolic_math'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", " \n", "cwd = os.getcwd()\n", "cwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## mkdir, cd, ls" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_dir = 'test_dir'\n", "os.mkdir(new_dir)\n", "os.chdir(new_dir)\n", "os.getcwd()\n", "os.listdir()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## globでfull_path表示" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import glob\n", "\n", "files = glob.glob(cwd+'/'+ new_dir+'/*')\n", "for file in files:\n", " print(file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# fileの読み書き" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/bob/python/doing_math_with_python/symbolic_math/test_dir'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 単純な読み書き" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data = ['x', 'y']\n", "text = \"\\n\".join(data)\n", "with open(\"tmp.txt\", \"w\") as f:\n", " f.write(text)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x\\n', 'y']\n" ] } ], "source": [ "with open(\"tmp.txt\", \"r\") as f:\n", " lines = f.readlines()\n", " print(lines)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x\n", "y\n" ] } ], "source": [ "for line in lines:\n", " print(line.rstrip())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## sprintfからの出力" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x', 0, 'y', 0]\n", " x: 0, y: 0\n", "\n", "['x', 1, 'y', 1]\n", " x: 1, y: 1\n", "\n" ] } ], "source": [ "data = [['x', 0, 'y', 0],\n", " ['x', 1, 'y', 1]]\n", "\n", "with open(\"tmp.txt\", \"w\") as f:\n", " for dd in data:\n", " print(dd)\n", " string = '%4s:%3d, %4s:%3d\\n' % tuple(dd)\n", " print(string)\n", " f.writelines(string)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['x', 0, 'y', 0]\n", " x: 0, y: 0\n", "\n", "['x', 1, 'y', 1]\n", " x: 1, y: 1\n", "\n" ] } ], "source": [ "data = [['x', 0, 'y', 0],\n", " ['x', 1, 'y', 1]]\n", "\n", "f = open(\"tmp.txt\", \"w\")\n", "for dd in data:\n", " print(dd)\n", " string = '%4s:%3d, %4s:%3d\\n' % tuple(dd)\n", " print(string)\n", " f.writelines(string)\n", "\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " x: 0, y: 0\n", " x: 1, y: 1\n" ] } ], "source": [ "file = open(\"tmp.txt\", \"r\")\n", "lines = file.readlines()\n", "for line in lines:\n", " print(line.rstrip())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.8.5" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "colors": { "hover_highlight": "#DAA520", "navigate_num": "#000000", "navigate_text": "#333333", "running_highlight": "#FF0000", "selected_highlight": "#FFD700", "sidebar_border": "#EEEEEE", "wrapper_background": "#FFFFFF" }, "moveMenuLeft": true, "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": true, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 2 }