Files

Table of Contents

1. computers

  • here are a few components of computers:
    • input/output devices
      • input ex: keyboard
      • output ex: screen or display.
      • also known as peripherals.
    • storage
      • solid-state drive (ssd)
      • non-volatile = store content even while computer is turned off.
    • memory
      • random-access memory (ram)
      • temporarily holds data.
      • faster than ssd or disk.
      • volatile = lose content when computer is turned off.
      • memory size is formatted in bits or bytes (8 bits).
      • megabytes (MB), gigabytes (GB), or terabytes (TB).
    • processor
      • runs computer programs.
      • contains a little bit of RAM called cache, which saves commonly used instructions/data.
    • clock
      • has specific frequency.
      • used to synchronize operations that are executed internally.
  • operating system (os) allows for multiple programs to be run at once.

2. command-line arguments

  • values passed to a source file when executed from the command line/terminal.
  • example:
# sample_file.txt can now be processed in
# example_program.py without typing out the filename
# during execution.
python example_program.py sample_file.txt

# the command line contents are stored in list
# sys.argv. In this example, sys.argv would contain:
sys.argv = ["example_program.py", "sample_file.txt"]

# recommended to check the length (number of
# arguments) of sys.argv before processing data.
# if an incorrect number of arguments are passed,
# print an error message for the user.
# all command-line arguments in sys.argv are strings.
# - must convert integers and floats for processing.

3. reading files

  • useful for:
    • large amounts of input.
    • saving user progress for later.
  • switching from receiving user input to receiving files as input.
# new file path
filename = "sample_file.txt"

# must explicitly open and close file streams.
# opens the files and returns a file object,
# which is used as an input stream.
input_file = open(filename)

# reads all the contents of the file object and returns a string.
file_contents = input_file.read()

# can manually close a file after reading its contents.
input_file.close()

# once you close a file, you can use functions on the
# file contents so the data can be processed.
def process(content):
    pass # replace with function implementation

# call the function, passing in the file contents as a
# parameter. you'll wanna get used to processing files
# this way since it's a common thing to do.

process(file_contents)

# supports filenames located in the same directory as
# the source file as well as full path names

path1 = "/home/user/documents/sample_file.txt"
path2 = "sample_file.txt"

input_file1 = open(path1)
input_file2 = open(path2)

# when reading from a file, you can also retrieve a
# list of strings, with each string being a line from
# the file.
file_contents = filename.readlines()

# or you can read one line at a time, which is useful
# for a file with a lot of content.
one_line = filename.readline()

# even supports for ... in ... syntax
for line in file_object:
    print(line)

4. writing files

# "w" means write mode. if you don't specify a mode,
# it defaults to read mode.
output_file = open("some_file.txt", "w")
output_file.write("Hello World!")
output_file.close()

# integers and floats must be converted to string when
# writing to a file.
output_file = open("another_file.txt", "w")
output_file.write(str(67))
output_file.close()

  • different open file modes:
    • “r” = read mode
    • “w” = write mode
      • if file doesn’t exist, a new file is created.
      • if file does exist, contents are overwritten.
    • “a” = append mode
      • if file doesn’t exist, a new file is created.
      • if file does exist, writes are appended to the existing content.
    • “r+” and “w+” = update mode
      • reading and writing to a file simultaneously.

5. with statement

  • with statement automatically closes a file after opening a file and executing statements.
    • a context manager.
  • use a with statement when opening files to ensure file is closed when done.
# writing to a file
with open("file_again.txt", "w") as file_object:
    file_object.write("Hello World!\n")
    file_object.write("ICS 31\n")

# reading to a file
with open("a_file.txt", "r") as file_object:
    contents = file_object.read()
    print(contents)

Author: Ashley Pock

Created: 11/25/2025 Tue