-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWibbleFinder.py
More file actions
56 lines (43 loc) · 1.84 KB
/
Copy pathWibbleFinder.py
File metadata and controls
56 lines (43 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Problem 0
# Previously, we learned Python's standard pattern for reading text files,
# as well as ways to take in input from the command line.
# We also know how to find matching strings within a line of text.
# Write a program called WibbleFinder.py that:
# 1) takes in the name of a text file from the command line;
# 2) reads that file;
# 3) counts how many occurrences of the substring "wibble" occur in the file;
# 4) prints the average number of wibbles per line.
# Your program should gracefully handle the cases where a)
# the user gives a file name that is not a valid file, or b) the file has zero lines.
# You can try your program out on the included files puck.txt
# Download puck.txt and an empty file (that you create) named empty_puck.txt.
class emptyFile(Exception):
pass
name = input("Give me the name of a text file: ")
# can only call files in this specific location
filename = "/Users/willapolman/Downloads/us_names/"+name+".txt"
# if this all goes well, fine and good, otherwise...
try:
f = open(filename)
line = f.readline()
countWibble = 0
countLineNums = 0
# this checks if the first line is empty
if (len(line.strip()) == 0 and countLineNums == 0):
raise emptyFile()
string = "wibble"
while (line):
# if the string "wibble" is found, the wibble counter goes up
if (string in line):
countWibble += 1
# always goes up regardless of wibble status
countLineNums += 1
line = f.readline()
print(str(countWibble) + " wibbles found.")
print("Average of " + str(countWibble/countLineNums) + " wibbles per line.")
# if the emptyFile flag is raised
except emptyFile:
print("There is nothing in this file...")
# if it fails out but the emptyFile flag isn't raised
except:
print("Something went wrong, I don't think that's a file avaliable.")