File Input and Output
Computers and Programming 101
Rutgers University - Newark
File Input and Output
Programs need to save data for
future use by writing it to a file.
The data can be read from the
file later, ensuring persistence
between program runs.
Data in RAM (e.g., variables) is
lost when the program stops. To
retain data between executions,
the program must save it to a
file.
File Input and Output
Files and Data Storage:
• Data saved in a file remains on the disk even after the
program ends.
• The data in a file can be retrieved for later use.
Examples of File Usage:
• Word Processors: Store documents (letters, memos, reports) in
files for future editing and printing.
• Image Editors: Save edited images or graphics in files.
• Spreadsheets: Store numerical data and formulas in files for
later use.
• Games: Save player scores or game states in files to continue
play later.
File Input and Output
Business Software:
• Many business applications use files to store critical data, such
as:
• Payroll
• Data Inventory
• Data Financial records
Writing Data to a File:
• The process of saving data in a file is called writing data to the
file.
• Output File: A file where data is written is called an "output
file" because it stores the program’s output.
Types of Text Files
Text Files:
• Contain data encoded as text (e.g., ASCII, Unicode).
• Even numbers are stored as text characters (e.g., "123"
instead of 123).
• Can be opened and viewed in a text editor (e.g., Notepad).
Binary Files:
• Contain data not converted to text; intended for program use
only.
• Cannot be viewed with a text editor.
Note: Python works with both text and binary files
How to Access Files
Sequential Access:
• Data is read from the beginning to the end of the file.
• To access data at the end, you must read through all prior
data.
• Example: Similar to a cassette tape where you must fast-forward to
reach later tracks.
Direct (Random) Access:
• Data can be accessed directly, without needing to read all
previous data.
• Example: Like a CD or MP3 player, where you can jump to any song
instantly.
Filenames and File Objects
Filenames:
• Files are identified by filenames, such as [Link],
[Link], or [Link].
• When saving or opening a file, the user specifies its filename.
File Objects:
• In Python, files are represented as file objects that are linked
to specific filenames for reading or writing.
Opening Files in Python
file_variable = open(filename, mode)
• file_variable: variable that references the file object.
• filename: string specifying the file name.
• mode: string specifying the file mode (e.g., read, write).
Opening Files in Python
File Modes:
• 'r': Open file for reading only (file cannot be modified).
• 'w': Open file for writing (creates file or overwrites existing file).
• 'a': Open file for appending (adds data to the end of the file, creates
file if it doesn't exist)
customer_file = open('[Link]', 'r’)
sales_file = open('[Link]', 'w')
Note: Using 'w' mode overwrites an existing file (deletes
its contents). Ensure you don't accidentally overwrite
important data.
Specifying the Location of a File
Same directory as the program:
• If no path is specified, Python assumes the file is in the same
directory as the program.
test_file = open('[Link]', 'w')
Different directory:
• Specify a full path to open the file elsewhere. On Windows, use a
raw string (r) to prevent escape sequences in the path.
test_file = open(r'C:\Users\Blake\temp\[Link]', 'w')
Writing Data to a File
Writing Data:
• Use the file object's write() method to write data to a file.
file_variable.write(string)
customer_file.write('Charles Pace')
You can write string or numeric data to the file. Writing variable data:
name = 'Charles Pace'
customer_file.write(name)
Closing a File
Why Close a File?:
• Closing a file is essential to ensure that all data is saved.
• Data is first written to a buffer in memory and may not be
written to the disk until the file is closed
Closing a File in Python:
• Use the close() method to close the file and ensure all data
is written.
customer_file.close()