Reworked snippets for opening and closing a file to use 'with open' instead of open/close

This commit is contained in:
Donne Martin 2015-02-16 17:11:42 -05:00
parent 8b974b811a
commit f591857c77

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:c535c4eb558966d14a178dfbd21918ecadc105f12b3c5f939ebf807d5afd1e6f" "signature": "sha256:f23d7a0b01d6b36ad881c51726d1c5e72a1c2ee783560c6d984d2742ed199266"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -14,47 +14,28 @@
"source": [ "source": [
"# Files\n", "# Files\n",
"\n", "\n",
"* Open a File for Reading\n",
"* Read a File\n", "* Read a File\n",
"* Write to a New File\n", "* Write a File\n",
"* Read and write UTF-8" "* Read and Write UTF-8"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Open a File for Reading\n",
"\n",
"Open a file in read-only mode:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"path = 'type_util.py'\n",
"f = open(path)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Read a File\n", "## Read a File\n",
"\n", "\n",
"Read the entire file with readlines. Iterate over the file lines as you would with a list. rstrip removes the EOL markers." "Open a file in read-only mode.<br\\>\n",
"Iterate over the file lines. rstrip removes the EOL markers.<br\\>"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"for line in f:\n", "old_file_path = 'type_util.py'\n",
"with open(old_file_path, 'r') as old_file:\n",
" for line in old_file:\n",
" print(line.rstrip())" " print(line.rstrip())"
], ],
"language": "python", "language": "python",
@ -91,30 +72,29 @@
] ]
} }
], ],
"prompt_number": 3 "prompt_number": 1
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Write to a New File\n", "## Write to a file\n",
"\n", "\n",
"Create a new file (overwriting any previous file with the same name), write text then close the file:" "Create a new file overwriting any previous file with the same name, write text, then close the file:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"path = 'hello_world.txt'\n", "new_file_path = 'hello_world.txt'\n",
"f = open(path, 'w')\n", "with open(new_file_path, 'w') as new_file:\n",
"f.write('hello world!')\n", " new_file.write('hello world!')"
"f.close()"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 4 "prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -136,7 +116,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 5 "prompt_number": 3
} }
], ],
"metadata": {} "metadata": {}