{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A **text file** is simply a sequence of characters and a binary file (such as an image or video) is a sequence of bytes. A file terminated with a end-of-file marker.\n", "#### In order to read or write a file, your program must first **open** the file. It should **close** the file when it's done with the file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9.3.1 Writing to a Text File; Introducing the `with` Statement " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the `with` statement to simplify working with text files. Python imposes no structure on a text file, so it's up to us to format the file according to the program's requirements. For example, put line feed characters to break the file into separate lines, one per record.\n", "#### An example to write five records to textfile `accounts.txt`. The keyword argument `mode='w'` specifies to **write** to a new text file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open('accounts.txt', mode='w') as accounts:\n", " accounts.write('100 Jones 24.98\\n')\n", " accounts.write('200 Doe 345.67\\n')\n", " accounts.write('300 White 0.00\\n')\n", " accounts.write('400 Stone -42.16\\n')\n", " accounts.write('500 Rich 224.62\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The `with` Statement\n", "#### The `with` statement performs several operations for our program:\n", "- Open the file and associate it with a **file object** (named `accounts` in the example).\n", "- Enable the program to access the file via the file object.\n", "- Close the file when the program is done executig the `with` statement's suite." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-In Functions `open()` and `close()`\n", "#### If we don't want to use `with`, can explicitly call `open()` and `close()`. Here, the keyword argument `mode='a'` specifies to **append** to an existing file. We can also write to the file with `print()` which adds the line feed by default." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accounts_file = open('accounts.txt', mode='a')\n", "\n", "accounts_file.write('600 Wilson 53.7\\n')\n", "accounts_file.write('700 Twain 101.8\\n')\n", "\n", "print('800 Penrose -72.45', file=accounts_file)\n", "print('900 Ross 88.88', file=accounts_file)\n", "\n", "accounts_file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2024 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }