9.4 Updating Text Files

  • Formatted data written to a text file cannot be modified without the risk of destroying other data
  • If the name 'White' needs to be changed to 'Williams' in accounts.txt, the old name cannot simply be overwritten
  • The original record for White is stored as
    300 White 0.00
    
  • If you overwrite the name 'White' with the name 'Williams', the record becomes
    300 Williams00
    
  • The characters beyond the second “i” in 'Williams' overwrite other characters in the line
  • The problem is that records and their fields can vary in size

9.4 Updating Text Files (cont.)

  • To make the preceding name change, we can:
    • copy the records before 300 White 0.00 into a temporary file,
    • write the updated and correctly formatted record for account 300 to this file,
    • copy the records after 300 White 0.00 to the temporary file,
    • delete the old file and
    • rename the temporary file to use the original file’s name.
  • Requires processing every record in the file, even if you need to update only one record
    • More efficient when an application needs to update many records in one pass of the file

Updating accounts.txt

  • Update the accounts.txt file to change account 300’s name from 'White' to 'Williams' as described above:
In [1]:
accounts = open('accounts.txt', 'r')
In [2]:
temp_file = open('temp_file.txt', 'w')
In [3]:
with accounts, temp_file:
    for record in accounts:
        account, name, balance = record.split()
        if account != '300':
            temp_file.write(record)
        else:
            new_record = ' '.join([account, 'Williams', balance])
            temp_file.write(new_record + '\n')

Updating accounts.txt (cont.)

  • This with statement manages two resource objects, specified in a comma-separated list after with
    • If the account is not '300', we write record (which contains a newline) to temp_file
    • Otherwise, we assemble the new record containing 'Williams' in place of 'White' and write it to the file
In [4]:
# macOS/Linux Users: View file contents
!cat temp_file.txt
100 Jones 24.98
200 Doe 345.67
300 Williams 0.00
400 Stone -42.16
500 Rich 224.62
In [ ]:
# Windows Users: View file contents
!more temp_file.txt

os Module File-Processing Functions

  • To complete the update, delete the old accounts.txt file, then rename temp_file.txt as accounts.txt
In [5]:
import os
In [6]:
os.remove('accounts.txt')
  • Use the rename function to rename the temporary file as 'accounts.txt'
In [7]:
os.rename('temp_file.txt', 'accounts.txt')
In [8]:
# macOS/Linux Users: View file contents
!cat accounts.txt
100 Jones 24.98
200 Doe 345.67
300 Williams 0.00
400 Stone -42.16
500 Rich 224.62
In [ ]:
# Windows Users: View file contents
!more accounts.txt

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.