files and strings (slides)

CSc 110 - files and strings

Review - iterating lines in a file

info = open('info.txt', 'r')
for line in info:
    print(line)
info.close()
The quick brown fox

jumped over

the lazy

bear

sitting by the tree

Review - string methods

  • string.strip(chars) – removes any of the characters in chars from the beginning or end of string, returns a string
  • string.split(chars) – splits string at the chars, returns a list
  • string.lower() – forces all characters to lowercase, returns a string

Write a function

It reads the file with filename, iterates over each line in the numbers.txt and return the sum. Submit your attendance.

numbers.txt

1
0
5
7
2

sum_numbers.py

assert sum_all("numbers.txt") == 15

Write a function – solution

def sum_all(file_name):
  total = 0
  f = open(file_name, "r")
  for line in f:
    number = int(line.strip("\n"))
    total += number
  return total

def main():
  assert sum_all("numbers.txt") == 15

main()

Modes

  • To read a file:

    • Use ‘r’ for reading the contents of a file
  • To write to a file:

    • Use ‘a’ to append to the existing file content

    • Use ‘w’ to replace existing content and write to a file

Write function

# opened the file in w mode
f = open('words.txt', 'w')

# write text content to the file
f.write("Let us start a story.")

# close the file
f.close()

You will see words.txt in your folder.

Let us start a story.

Evaluate the code

f = open('words.txt', 'w')
f.write('The slow wolf')
f.write('jumped over')
f.write('the bear')
f.close()

Evaluate the code

f = open('words.txt', 'w')
f.write('The slow wolf')
f.write('jumped over')
f.write('the bear')
f.close()

words.txt

The slow wolfjumped overthe bear

Evaluate the code

f = open('words.txt', 'w')
f.write('The slow wolf\n')
f.write('jumped over\n')
f.write('the bear')
f.close()

Evaluate the code

f = open('words.txt', 'w')
f.write('The slow wolf\n')
f.write('jumped over\n')
f.write('the bear')
f.close()

words.txt

The slow wolf
jumped over
the bear

Evaluate the code – continued

f = open('words.txt', 'a')
f.write('The quick fox\n')
f.write('jumped over\n')
f.write('the bear')
f.close()

Evaluate the code – continued

f = open('words.txt', 'a')
f.write('The quick fox\n')
f.write('jumped over\n')
f.write('the bear')
f.close()

words.txt

The slow wolf
jumped over
the bearThe quick fox
jumped over
the bear

Write key, value to a file

Replace line 4 and line 5 with your code

info = {"a": 2, "b": 5, "c": 0}
output = open("count.txt", "w")

# write code
# write code
output.write("end")

output.close()

count.txt

a, 2
b, 5
c, 0
end   

Write key, value to a file

info = {"a": 2, "b": 5, "c": 0}
output = open("count.txt", "w")

for key, value in info.items():
  output.write(key + ", " + str(value) + "\n")
output.write("end")

output.close()

count.txt

a, 2
b, 5
c, 0
end   

Write two functions

  • Both functions takes a string file_name as argument:
    • count_words count frequency of each lowercase word, return a dictionary.
    • write_word_count call count_words, iterate it and write a comma separated file (“out_” + file_name).
  • Download test file. Name your file as read_and_write.py.

Test case:

write_word_count("alien.txt")
# writes to out_alien.txt with word counts

Write two functions – solution

def count_words(filename):
  f = open(filename, "r")
  counts = {}
  for line in f:
    words = line.strip("\n").split(" ")
    for w in words:
      lower_w = w.lower()
      if lower_w not in counts:
        counts[lower_w] = 1
      else:
        counts[lower_w] += 1
  return counts

def write_word_count(filename):
  count_dict = count_words(filename)
  output_file = open("out_" + filename, "w")
  for key, value in count_dict.items():
    output_file.write(key + "," + str(value) + "\n")
  output_file.close()
  
def main(): 
  write_word_count("alien.txt")
  
main()