{ "cells": [ { "cell_type": "markdown", "id": "5f279941", "metadata": {}, "source": [ "# Files! You gotta love them!" ] }, { "cell_type": "code", "execution_count": 10, "id": "364d76b4", "metadata": {}, "outputs": [], "source": [ "myfile = open('foo.txt')\n", "data = myfile.read()\n", "myfile.close()" ] }, { "cell_type": "code", "execution_count": 11, "id": "5592691e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello there!'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] }, { "cell_type": "code", "execution_count": 12, "id": "27ac7b1f", "metadata": {}, "outputs": [ { "ename": "UnsupportedOperation", "evalue": "not writable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mUnsupportedOperation\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [12]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m myfile \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfoo.txt\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m myfile\u001b[38;5;241m.\u001b[39mwrite(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello there!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m myfile\u001b[38;5;241m.\u001b[39mclose()\n", "\u001b[0;31mUnsupportedOperation\u001b[0m: not writable" ] } ], "source": [ "myfile = open('foo.txt','w')\n", "myfile.write(\"Hello there!\")\n", "myfile.close()" ] }, { "cell_type": "code", "execution_count": 19, "id": "b530bb00", "metadata": {}, "outputs": [], "source": [ "myfile = None\n", "def step1():\n", " global line\n", " myfile = open(\"foo.txt\",\"w\")\n", " myfile.write(\"222 This is line\")\n", " myfile.write(\"555 This is another line\")\n", "def step2():\n", " myfile = open(\"foo.txt\")\n", " print(\"I read\",myfile.read())" ] }, { "cell_type": "code", "execution_count": 20, "id": "130faef7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I read 222 This is line555 This is another line\n" ] } ], "source": [ "step1()\n", "step2()" ] }, { "cell_type": "code", "execution_count": 21, "id": "c39d3d8a", "metadata": {}, "outputs": [], "source": [ "primes = [2,3,5,7,9]" ] }, { "cell_type": "code", "execution_count": 22, "id": "d6e5a33a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes[0:3]" ] }, { "cell_type": "code", "execution_count": 23, "id": "d023860a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 9]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes[3:6]" ] }, { "cell_type": "code", "execution_count": 24, "id": "057ae73d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7, 9]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes[0:3] + primes[3:6]" ] }, { "cell_type": "code", "execution_count": 25, "id": "d49440f1", "metadata": {}, "outputs": [], "source": [ "# primes[a:b] + primes[b:c] == primes[a:c]" ] }, { "cell_type": "code", "execution_count": 26, "id": "309ae584", "metadata": {}, "outputs": [], "source": [ "primes.remove(9)" ] }, { "cell_type": "code", "execution_count": 27, "id": "8e305033", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes" ] }, { "cell_type": "code", "execution_count": 28, "id": "2e78fb40", "metadata": {}, "outputs": [], "source": [ "p2 = list(primes)\n" ] }, { "cell_type": "code", "execution_count": 29, "id": "18c2aaef", "metadata": {}, "outputs": [], "source": [ "p2.append(11)" ] }, { "cell_type": "code", "execution_count": 30, "id": "45c19b44", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes" ] }, { "cell_type": "code", "execution_count": 31, "id": "48d42e3d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 7, 11]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p2" ] }, { "cell_type": "code", "execution_count": 32, "id": "ab730d02", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "H\n", "e\n", "l\n", "l\n", "o\n", " \n", "t\n", "h\n", "e\n", "r\n", "e\n", "!\n", "\n", "\n", "T\n", "h\n", "i\n", "s\n", " \n", "i\n", "s\n", " \n", "a\n", " \n", "f\n", "i\n", "l\n", "e\n", ".\n", "\n", "\n", "I\n", " \n", "w\n", "a\n", "n\n", "t\n", " \n", "a\n", " \n", "b\n", "u\n", "n\n", "c\n", "h\n", " \n", "o\n", "f\n", " \n", "l\n", "i\n", "n\n", "e\n", "s\n", " \n", "i\n", "n\n", " \n", "i\n", "t\n", ".\n", "\n", "\n", "T\n", "h\n", "i\n", "s\n", " \n", "w\n", "a\n", "y\n", " \n", "I\n", " \n", "c\n", "a\n", "n\n", " \n", "d\n", "o\n", " \n", "a\n", "n\n", " \n", "e\n", "x\n", "a\n", "m\n", "p\n", "l\n", "e\n", ".\n", "\n", "\n" ] } ], "source": [ "for row in open('foo.txt').read():\n", " print(row)" ] }, { "cell_type": "code", "execution_count": 33, "id": "38c86f1d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello there!\n", "This is a file.\n", "I want a bunch of lines in it.\n", "This way I can do an example.\n", "\n" ] } ], "source": [ "for row in open('foo.txt').read().split(\"\\n\"):\n", " print(row)" ] }, { "cell_type": "code", "execution_count": 34, "id": "16616a4e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello there!',\n", " 'This is a file.',\n", " 'I want a bunch of lines in it.',\n", " 'This way I can do an example.',\n", " '']" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open('foo.txt').read().split(\"\\n\")" ] }, { "cell_type": "code", "execution_count": 35, "id": "a3ca27ed", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Hello there!\\n',\n", " 'This is a file.\\n',\n", " 'I want a bunch of lines in it.\\n',\n", " 'This way I can do an example.\\n']" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open('foo.txt').readlines()" ] }, { "cell_type": "code", "execution_count": 36, "id": "8cf8e2e4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello there!\\n'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open('foo.txt').readline()" ] }, { "cell_type": "code", "execution_count": 37, "id": "fdc95a75", "metadata": {}, "outputs": [], "source": [ "myfile = open('foo.txt')" ] }, { "cell_type": "code", "execution_count": 38, "id": "c0444f41", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello there!\\n'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfile.readline()" ] }, { "cell_type": "code", "execution_count": 39, "id": "4b4fb9a9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is a file.\\n'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfile.readline()" ] }, { "cell_type": "code", "execution_count": 40, "id": "3458398d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'I want a bunch of lines in it.'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "myfile.readline().strip()" ] }, { "cell_type": "code", "execution_count": 52, "id": "0685b8db", "metadata": {}, "outputs": [], "source": [ "def writeNumbers(name,a,b):\n", " myfile = open(name,'w')\n", " myfile.write(str(a)+\"\\n\")\n", " myfile.write(str(b)+\"\\n\")\n", " myfile.close()\n", "def averageNumbers(name):\n", " myfile = open(name)\n", " a = int(myfile.readline())\n", " b = int(myfile.readline())\n", " print(\"The average is\",(a+b)/2)" ] }, { "cell_type": "code", "execution_count": 50, "id": "8a3cb1fe", "metadata": {}, "outputs": [], "source": [ "writeNumbers(\"test.txt\",10,20)" ] }, { "cell_type": "code", "execution_count": 53, "id": "d59c4e46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The average is 15.0\n" ] } ], "source": [ "averageNumbers(\"test.txt\")" ] }, { "cell_type": "code", "execution_count": null, "id": "720b1469", "metadata": {}, "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }