San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 9.4 Updating Text Files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Updating `accounts.txt` "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dump_textfile(file_name):\n",
" \"\"\"\n",
" accounts.read() returns the entire file contents\n",
" as a single string.\n",
" \"\"\"\n",
" textfile = open(file_name, mode='r')\n",
" print(textfile.read())\n",
" textfile.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dump_textfile('accounts.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Suppose we want to replace 'Jones' with 'Abernathy'."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read the entire contents of the file into a string.\n",
"accounts = open('accounts.txt', mode='r')\n",
"contents = accounts.read()\n",
"accounts.close()\n",
"\n",
"# Modify the string.\n",
"contents = contents.replace('Jones', 'Abernathy')\n",
"\n",
"# Write the modified contents to the file,\n",
"# overwriting the original contents.\n",
"accounts = open('accounts.txt', mode='w')\n",
"accounts.write(contents)\n",
"accounts.close()\n",
"\n",
"dump_textfile('accounts.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### If the file is large, we may not want to read all of its contents into a string. Instead, we can make a temporary copy of the file. When we reach the line we to modify, we can make the replacement. For example, replace 'White' with 'Williams' in account 300:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"accounts = open('accounts.txt', 'r')\n",
"temp_file = open('temp_file.txt', 'w')\n",
"\n",
"with accounts, temp_file:\n",
" for record in accounts:\n",
" account, name, balance = record.split()\n",
" \n",
" if account != '300':\n",
" temp_file.write(record)\n",
" else:\n",
" new_record = ' '.join([account, 'Williams', balance])\n",
" temp_file.write(new_record + '\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `os` Module File Processing Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use the `os` (operating systems interface) module to remove file `accounts.txt` and rename file `temp_file.txt` to `accounts.txt`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.remove('accounts.txt')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.rename('temp_file.txt', 'accounts.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Command-Line Arguments\n",
"#### If you run a standalone Python program from the command line, you can pass arguments to the program. For example:\n",
"```\n",
"ipython copy_file.py accounts.txt backup.txt\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Your program can use the `sys` module to retrieve the values of the command-line arguments from the module's `argv` list. For example:\n",
"``` python\n",
"source_file_name = argv[0]\n",
"target_file_name = argv[1]\n",
"```"
]
},
{
"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 2023 by Ronald Mak"
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}