Python (Fall 2026) Module 12-8: File Challenge 2
Watch on YouTube →
Overview
Bill Moseley demonstrates permanent storage for a Python ATM program using a plain-text file, `ATM_data.txt`, instead of pickle for its fixed savings and checking variables. He builds `saveData` and `loadData` functions, handles first-run file creation with `os.path.exists`, and shows how saving after account changes preserves balances between sessions.
Key takeaways
- For a Python program with only fixed savings and checking variables, a two-line text file can store the balances without the added complexity of pickle.
- Convert numeric balances to strings before calling `file.write`, and convert the lines back to floats after reading them with `readline`.
- Save the full current account state after every transaction that changes a balance, using write mode to replace the file's previous values.
- Check `os.path.exists("ATM_data.txt")` before loading; if the file is absent, initialize balances and save them to establish the file.
- Variables assigned inside `loadData` are local unless declared `global` or returned, so the example uses `global` to update balances used elsewhere in the ATM program.
- Opening a data file only for the brief read or write operation and closing it afterward limits the time the file remains open.
Chapters
- The ATM starts with fixed savings and checking variables, so a plain-text file is sufficient; pickle is more useful for saving a structure such as a dictionary of accounts.
- The `saveData` function accepts both balances and opens `ATM_data.txt` in write mode, creating it if needed and replacing its prior contents.
- Each balance is converted to a string and written on a separate line so the values can be read back individually.
- Call `saveData` whenever a deposit or withdrawal changes an account balance.
- The `loadData` function reads the first and second lines of `ATM_data.txt` into the savings and checking balances, then converts them back to floats.
- Files are opened only while reading or writing and then closed; the example omits exception handling to keep the implementation simple.
- Because `loadData` fails if the file is missing, `os.path.exists` checks for `ATM_data.txt` before attempting to read it.
- On the first run, the program initializes both balances to zero and calls `saveData` to create the file for future sessions.
- The first run creates `ATM_data.txt` with the two initial balances; later runs can load the saved values instead of resetting them.
- A deposit test exposes a scope issue: assignments inside `loadData` create local variables rather than updating the account variables used by the ATM.
- Moseley resolves the example's scope issue with `global`, then demonstrates a savings deposit and a checking deposit being written to the file.
- The saved balances remain available for subsequent balance checks and program sessions.
Transcript excerpt
0:00 The other file challenge is to create an app with permanent storage. Like I said, you can use pickle, or you can just use a plain text file, depending on how you want to do things. Both are valid ways to do things. But you will need a text file to open the data and then save the data back to the file each time as a, a record is updated. So when the program starts up, you want to figure out a way to load the data from a previous session with that program in from a text file. And then anytime that data is updated, you want to save that back to the text file. So I'm going to show you using our ATM assignment. So here's my ATM, and it's not complete. I didn't actually fill out the withdrawal portion, but I can check balances and make deposits. So,
0:58 this is my ATM assignment. I've got my two variables here. Now, I don't really need, for two variables like this, I don't really need to have pickle. I could use pickle to save this with the two variables, but I don't need pickle to save this. The way I would use this with pickle is if I had a dictionary object that contained my accounts as dictionary entries, savings and an amount, checking and amount, I would use pickle for sure with that. With these two hard-coded variables, I know that my, there's always going to be just one savings and one checking account. So, I'll probably just use a regular text file, but they, the, the flow of things is still going to be the same. So, I've got my welcome message. I've got my while true where I have the menu. I can make a deposit. I can make a withdrawal. I can check the balance.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.