{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading and Writing files" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n", "\n", "Suspendisse vel risus a felis consectetur aliquet non ut mi. \n", "\n", "Quisque a posuere lorem.\n", "\n", "Curabitur nec arcu ligula.\n", "\n" ] } ], "source": [ "f = open(\"file-demo-read.txt\", \"r\")\n", "data = f.read()\n", "print(data)\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More clean way. The close method will be implicitly called just before exiting with block." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n", "\n", "Suspendisse vel risus a felis consectetur aliquet non ut mi. \n", "\n", "Quisque a posuere lorem.\n", "\n", "Curabitur nec arcu ligula.\n", "\n" ] } ], "source": [ "with open(\"file-demo-read.txt\", \"r\") as f:\n", " data = f.read()\n", "\n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading one line at a time" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n", "\n" ] } ], "source": [ "with open(\"file-demo-read.txt\", \"r\") as f:\n", " data = f.readline()\n", "\n", "print(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing into file" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "f = open(\"file-demo-write.txt\", \"r+\")\n", "f.write(\"This is a sample text.\")\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reading the written content" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a sample text.\n" ] } ], "source": [ "f = open(\"file-demo-write.txt\", \"r\")\n", "data = f.read()\n", "print(data)\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File positioning" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "is a sample text.\n", "This is a sample text.\n" ] } ], "source": [ "f = open(\"file-demo-write.txt\", \"r\")\n", "f.seek(5)\n", "data = f.readline()\n", "print(data)\n", "f.seek(0)\n", "data = f.readline()\n", "print(data)\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File system operations" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a directory" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "os.mkdir(\"demo\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rename a directory" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "os.rename(\"demo\", \"demo-dir\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change current working directory" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "curr_dir_backup = os.getcwd()\n", "os.chdir(\"demo-dir\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print current working directory" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/jovyan/demo-dir\n" ] } ], "source": [ "curr_dir = os.getcwd()\n", "print(curr_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a file under this directory" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "f= open(\"demo.txt\",\"w+\")\n", "f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Rename this file" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "os.rename(\"demo.txt\", \"demo-file.txt\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create two directory under this directory" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "os.mkdir(\"demo-sub-dir-1\")\n", "os.mkdir(\"demo-sub-dir-2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List all files and directories" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['demo-sub-dir-2', 'demo-file.txt', 'demo-sub-dir-1']\n" ] } ], "source": [ "dir_list = os.listdir()\n", "print(dir_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get the statistics of a director. Current directory in the case below." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "os.stat_result(st_mode=16877, st_ino=22774768, st_dev=1048804, st_nlink=4, st_uid=1000, st_gid=1000, st_size=4096, st_atime=1538149930, st_mtime=1538149928, st_ctime=1538149928)\n" ] } ], "source": [ "stat = os.stat(\".\")\n", "print(stat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Clean up" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "if(os.path.exists(\"../demo-dir\")):\n", " if(os.path.exists(\"demo-sub-dir-1\")):\n", " os.rmdir(\"demo-sub-dir-1\")\n", " if(os.path.exists(\"demo-sub-dir-2\")):\n", " os.rmdir(\"demo-sub-dir-2\")\n", " if(os.path.isfile(\"demo-file.txt\")):\n", " os.remove(\"demo-file.txt\")\n", " os.rmdir(\"../demo-dir\")\n", "os.chdir(curr_dir_backup)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }