open(..., 'w')
¶# see the file write_example.py
with open('write_example.txt', 'w') as file:
file.write('Hello?\n')
write.py
¶NOTES
write_example.txt
and examine its contents%%file write_more.py
with open('write_example.txt', 'w') as file:
file.write('Hello world!\n')
file.write('The ability to write files seems like it might be useful.\n')
write_more.py
¶NOTES
write_example.txt
and examine its contentsboring_file.txt
¶%%file boring_file.txt
This is a boring file.
It needs some pizzaz.
Like bullets at the front of each line.
%%file make_exciting.py
with open('boring_file.txt') as infile:
with open('exciting_file.txt', 'w') as outfile:
for line in infile:
outfile.write('⭐️ ')
outfile.write(line)
! cat exciting_file.txt
! cat boring_file.txt
NOTES
boring_file.txt
exciting_file.txt
'w'
to the open
of boring_file.txt
'w'
and run it again. It appears to work.excting_file.txt
. It is empty!boring_file.txt
. It is empty!!# Problem code!
with open('boring_file.txt', 'w') as infile:
with open('exciting_file.txt', 'w') as outfile:
for line in infile:
outfile.write('⭐️ ')
outfile.write(line)
When you open an existing file for writing, the original contents are DELETED.
With great power comes great responsibility
Measure twice, cut once.
NOTES
%%file write_lines.py
with open('another_example.txt', 'w') as file:
file.write('First line.')
file.write('Second line.')
file.write('Third line.')
write_lines.py
¶NOTES
write_lines.py
another_example.txt
file.write('\n')
with open('another_example.txt', 'w') as file:
file.write('First line.')
file.write('\n')
file.write('Second line.')
file.write('\n')
file.write('Third line.')
file.write('\n')
NOTES
interpolation: the insertion of something into something else
name = 'Dr. Bean'
print(f'Hello, my name is {name}')
NOTES
{...}
allow you to include any expression. The resulting value will be turned into a string an included at that location.f
at the beginning is important. What happens without it?def print_info(name, age, favorite_pizza):
print(f"{name} is {age} years old. \n{name}'s favorite pizza is {favorite_pizza}. 🍕")
print_info()
print_info()
print_info('Jon', 21, "buffalo chicken") # <- Fill in
print_info('Wyatt', 22, 'pepperoni') # <- Fill in
print_info('Gordon', 38, 'alfredo hawaiian')
Given a name, pronoun, major, and hometown, write a function that would write out the information in the following format:
Alice is from Beaverdam.
At BYU, she is majoring in Sociology.
Do you know anyone else from Beaverdam?
def print_info(name, pronoun, major, hometown):
pass
print_info('Alice', 'she', 'Sociology', 'Beaverdam')
# Solution
def print_info(name, pronoun, major, hometown):
print(f'{name} is from {hometown}.')
print(f'At BYU, {pronoun} is majoring in {major}.')
print(f'Do you know anyone else from {hometown}?')
print_info('Alice', 'she', 'Sociology', 'Beaverdam')
Now lets write that to a file instead of printing it to the console.
def print_info(file, name, pronoun, major, hometown):
file.write(f"{name} is from {hometown}.\nAt BYU, {pronoun} is majoring in {major}.\n")
file.write(f"Do you know anyone else from {hometown}?\n")
with open('information.txt', 'w') as outfile:
print_info(outfile, 'Jason', 'he', 'Computer Science', 'someplace in Alaska 🥶')
print_info(outfile, 'Isabella', 'she', 'Computer Science', 'Arizona 🥵')
with open('information.txt') as infile:
for line in infile:
print(line)
# Solution
def print_info(file, name, pronoun, major, hometown):
file.write(f'{name} is from {hometown}.\n')
file.write(f'At BYU, {pronoun} is majoring in {major}.\n')
file.write(f'Do you know anyone else from {hometown}?\n')
with open('all_about_alice.txt', 'w') as file:
print_info(file, 'Alice', 'she', 'Sociology', 'Beaverdam')
Do you pass a file name to print_info
or a file handle?
NOTES
print_info
open a fileprint_info
is focused on writing specific informationWe let some other part of the code decide where that information gets written
Demonstrate how we can easily add information for more students to the same file.
with open('numbers.txt', 'w') as file:
number = 42
file.write(f"the number is {number}")
file.write('\n')
NOTES
In addition to iterating over all the elements of a list, it is also helpful to be able to access a specific element, like the first, or third.
fruits = ['apples', 'peaches', 'strawberries', 'plums']
fruits[3]
fruits[0]
fruits[3]
NOTES
[]
immediately following a list, with an integer inside.[0]
, not by [1]
fruits[2.5]
fruits[]
len(fruits)
fruits[4]
NOTES
length - 1
sys.argv
¶argv_demo.py
¶! python argv_demo.py
NOTES
! python argv_demo.py and some more stuff
copy_file.py
¶Write a program that copies a file.
For example,
python copy_file.py some_file.txt another_file.txt
should write a file named another_file.txt
that has a copy of the contents of some_file.txt
.
NOTES
copy_file.py
print
sys.argv
- get index 1 and 2, not index 0 and 1python copy_file.py copy_file.py copy_again.py
)%%file contents.txt
This file has stuff in it.
Stuff that you'd probably like more copies of.
If only you had a program that could copy files...
import sys
def read_lines(filename):
with open(filename) as file:
return list(file)
def write_lines(filename, lines):
with open(filename, 'w') as file:
for line in lines:
file.write(line)
def main(input_file, output_file):
lines = read_lines(input_file)
write_lines(output_file, lines)
main(sys.argv[1], sys.argv[2])
!python copy_file.py contents.txt more_contents.txt
! cat more_contents.txt
open(..., 'w')
for writing to a file'\n'
represents the newline characterf'...{...}...'
string interpolationmy_list[...]
sys.argv