{
"metadata": {
"name": "",
"signature": "sha256:f23d7a0b01d6b36ad881c51726d1c5e72a1c2ee783560c6d984d2742ed199266"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Files\n",
"\n",
"* Read a File\n",
"* Write a File\n",
"* Read and Write UTF-8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read a File\n",
"\n",
"Open a file in read-only mode.
\n",
"Iterate over the file lines. rstrip removes the EOL markers.
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"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())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"class TypeUtil:\n",
"\n",
" @classmethod\n",
" def is_iterable(cls, obj):\n",
" \"\"\"Determines if obj is iterable.\n",
"\n",
" Useful when writing functions that can accept multiple types of\n",
" input (list, tuple, ndarray, iterator). Pairs well with\n",
" convert_to_list.\n",
" \"\"\"\n",
" try:\n",
" iter(obj)\n",
" return True\n",
" except TypeError:\n",
" return False\n",
"\n",
" @classmethod\n",
" def convert_to_list(cls, obj):\n",
" \"\"\"Converts obj to a list if it is not a list and it is iterable,\n",
" else returns the original obj.\n",
" \"\"\"\n",
" if not isinstance(obj, list) and cls.is_iterable(obj):\n",
" obj = list(obj)\n",
" return obj\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write to a file\n",
"\n",
"Create a new file overwriting any previous file with the same name, write text, then close the file:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"new_file_path = 'hello_world.txt'\n",
"with open(new_file_path, 'w') as new_file:\n",
" new_file.write('hello world!')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read and Write UTF-8"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import codecs\n",
"with codecs.open(\"hello_world_new.txt\", \"a\", \"utf-8\") as new_file:\n",
" with codecs.open(\"hello_world.txt\", \"r\", \"utf-8\") as old_file: \n",
" for line in old_file:\n",
" new_file.write(line + '\\n')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
}
],
"metadata": {}
}
]
}