On (at least most of) our computers, there is a file system via which we can create, save, modify, and remove files
On Mac: can browse with Finder
On Windows: can browse with windows explorer
File systems are often hierarchical
To open a file in a python program:
a_file = open(file_name, mode)
file_name should be the name of the file to open
mode should be the mode in which to open in
After opening, use the a_file object to read from or write to the file
A path describes a location.
An absolute path describes the location from the root directory.
A relative path describes the location of a file relative to the current (working) directory.
info.txt
read_file.py
file.readline()
reads one line from a file, returns a string
returns an empty string if it has reached the end of the content
file.readlines()
reads all of the lines
returns a list of strings
file.read()
info.txt
read_line.py
Submit the read_first_line
function to Gradescope for attendance (given a file name string, open file in read mode, read first line, return that first line).
Name your file first_read.py
The type file
is iterable so we can use a for x in file
loop:
We worked with .isnumeric()
, a string method to check if a string contains only digit characters
Here are some other useful string methods:
string.strip(chars)
– removes any of the characters in chars from the beginning or end of string, returns a stringstring.split(chars)
– splits string at the chars, returns a list.strip()
info = open('info.txt', 'r')
first_line = info.readline()
second_line = info.readline()
print(first_line)
print(second_line)
first_line = first_line.strip("\n")
second_line = second_line.strip("\n")
print(first_line)
print(second_line)
info.close()
The quick brown fox
jumped over
The quick brown fox
jumped over
.split(" ")
info = open('info.txt', 'r')
first_line = info.readline().strip("\n")
second_line = info.readline().strip("\n")
first_line_words = first_line.split(" ")
second_line_words = second_line.split(" ")
print(first_line_words)
print(second_line_words)
info.close()
['The', 'quick', 'brown', 'fox']
['jumped', 'over']
lines_and_words
file_name
file_name
in read modeTest case (download text file):
def lines_and_words(file_name):
f = open(file_name, "r")
all_words = []
for line in f:
all_words.append(line.strip("\n").split(" "))
f.close()
return all_words
def main():
assert lines_and_words("info.txt") == [ ['The', 'quick', 'brown', 'fox'],
['jumped', 'over'], ['the', 'lazy'],
['bear'], ['sitting', 'by', 'the', 'tree'] ]
main()
count_words
file_name
as argumentTest case:
def count_words(file_name):
f = open(file_name, "r")
counts = {}
for line in f:
words = line.strip("\n").split(" ")
for w in words:
lower_case_w = w.lower()
if lower_case_w not in counts:
counts[lower_case_w] = 1
else:
counts[lower_case_w] += 1
return counts
def main():
assert count_words("info.txt") == {"the": 3, "quick": 1, "brown": 1,
"fox": 1 , "jumped": 1, "over": 1,
"lazy": 1, "bear": 1, "sitting": 1,
"by": 1, "tree": 1}
main()