Date

Lecture Date: Monday, October 17

We'll start by finishing up our debate analysis code.

Reading files is great, but what if we want to write stuff to disk? How can we do this... and not blow up our computers? Consider what would happen if we created an infinite loop....

Writing a file is very similar to calling print(), but we use the function my_file.write() instead. Other than that, it basically works the same.

Can we get information about the file/folder structure on the disk? Yes!

import os

print(os.listdir(os.getcwd()))

total_size = 0
for filename in os.listdir(os.getcwd()):
      total_size = total_size + os.path.getsize(os.path.join(os.getcwd(),filename))

print(total_size)

output_file = open("output_file.txt", "w")

output_file.write(str(os.listdir(os.getcwd())))
output_file.write('\n')
output_file.write(str(total_size))
output_file.close()

Completed debate program:

import string

def load_debate(filename):
   # read the debate file
   # return a list of lists where each item is one line from the file
   datafile = open(filename, "r")

   list_of_lines = []

   for line in datafile:
       new_line = line.strip().split(";")
       list_of_lines.append(new_line)

   return list_of_lines


def find_interruptions(debate):

   interruptions = {}

   for line in debate:
       if "..." in line[2]:
           if line[1] in interruptions:
               interruptions[line[1]] += 1
           else:
               interruptions[line[1]] = 1

   return interruptions

def word_finder(debate, speaker, word_to_find):

   count = 0
   for line in debate:
       if line[1] == speaker:
           if word_to_find in line[2].lower():
               full_line = line[2].lower()
               no_punctuation_line = ""
               for char in full_line:
                   if char not in string.punctuation:
                       no_punctuation_line += char
               no_punctuation_line_list = no_punctuation_line.split()
               for word in no_punctuation_line_list:
                   if word == word_to_find:
                       count += 1

   return count


debate = load_debate("first_debate.csv")
inter = find_interruptions(debate)
print(inter)
print("Trump Wrongs:", word_finder(speaker="Trump", debate=debate, word_to_find="wrong"))
print("Clinton Wrongs:", word_finder(debate, "Clinton", "american"))
print("Holt Wrongs:", word_finder(debate, "Holt", "please"))

Todo List program (up through the end of class):

todo_list = []

def read_todo_list():
   datafile = open("todo_list.txt", "r")
   for line in datafile:
       todo_list.append(line.strip())

   return todo_list

def add_to_list(item):
   todo_list.append(item)

def write_todo_list_file():
   # 1 - open the file
   # 2 - loop over the list
   # 3 - close

   datafile = open("todo_list.txt", "w")
   for item in todo_list:
       datafile.write(item)
       datafile.write("\n")
   datafile.close()


   return

print(read_todo_list())
add_to_list('Pet a goat')
write_todo_list_file()

Shopping List program (we didn't get to this, but it is here as an example):

shopping_list = []

# Read the file into the list
datafile = open("shopping_list.txt", "r")

for line in datafile:
    line = line.strip()
    shopping_list.append(line)
datafile.close()

print("Your current shopping list is:")
for item in shopping_list:
    print(item)

print()
while True:
    item_to_add = input("Items to add (NONE to stop): ")
    if item_to_add.upper() == "NONE":
        break
    shopping_list.append(item_to_add)

print()
while True:
    item_to_remove = input("Items to remove (NONE to stop): ")
    if item_to_remove.upper() == "NONE":
        break
    shopping_list.remove(item_to_remove)

print()
print("Your current shopping list is:")
for item in shopping_list:
    print(item)

print()
print("Saving to shopping_list.txt...")
datafile = open("shopping_list.txt", "w")

for item in shopping_list:
    datafile.write(item + "\n")