Skip to content

Commit cc73b5d

Browse files
committed
updatingch_8 and Ch_7
1 parent 4ef3105 commit cc73b5d

File tree

10 files changed

+2076
-0
lines changed

10 files changed

+2076
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# counting lines in a file
2+
3+
# example
4+
fhand = open('mbox.txt')
5+
count = 0
6+
for line in fhand:
7+
count = count + 1
8+
print('Line Count:', count)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# prompt user for file name
2+
3+
fname = input('Enter the file name: ')
4+
fhand = open(fname)
5+
count = 0
6+
for line in fhand:
7+
if line.startswith('Subject:') :
8+
count = count + 1
9+
print('There were', count, 'subject lines in', fname)
10+
11+
12+
# example output:
13+
# enter the filen name: mbox.txt
14+
# there were 1797 subject lines in mbox.txt
15+
16+
# Enter the the file name: mbox-short.txt
17+
# There were 27 subject lines in mbox-short.txt
18+
19+
# add a try except to the above to handle bad filename input
20+
21+
fname = input('Enter the file name: ')
22+
try: # added a try / except block where danger point is (filename input)
23+
fhand = open(fname)
24+
except:
25+
print('File cannot be opened:', fname)
26+
quit()
27+
28+
count = 0
29+
for line in fhand:
30+
if line.startswith('Subject:') :
31+
count = count + 1
32+
print('There were', count, 'subject lines in', fname)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# newline character
2+
# indicates when a line ends
3+
# \n
4+
5+
# n.b.: 'print' adds an additional \n at the end. (you can override this behaviour)
6+
stuff = 'Hello\nWorld!'
7+
print(stuff)
8+
9+
stuff2 = 'X\nY'
10+
print(stuff2)
11+
print(len(stuff))
12+
print(len(stuff2)) # this gives 3 characters because the \n is also a character
13+
14+
15+
# a text file has a newline \n at the end of each line
16+
#texttexttextn\
17+
#texttexttexttexttextn\
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# file handle as a sequence
2+
3+
# a file handle open for read can be treated as a sequence
4+
# of strings where each line in he file is a
5+
# string in the sequence.
6+
7+
# we can use a for statement to iterate through a sequence
8+
9+
# remember a sequence is an ordered set
10+
11+
xfile = open('mbox.txt')
12+
for cheese in xfile: # cheese is an iteration var for the for loop here
13+
print(cheese) # ' for each line (cheese) in the the filehandle 'xfile'
14+
# 'run this loop 1x, then print it'

Python4Everybody/Ch_7_Files/open.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# open() function
2+
# makes it possible to read the file
3+
# it returns a file handle (a variable) used to perform operations on the file
4+
5+
# handle = open(filename, mode) - takes atleast one parameter(name of file)
6+
# additional parameters are optional (e.g. mode / 'r' for read, 'w' for write etc)
7+
8+
# handle is a thing that allows you to get to the file
9+
#example:
10+
# fhand = open('mbox.txt')
11+
# print(fhand)

Python4Everybody/Ch_7_Files/skipin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# using 'in' operater to select lines
2+
3+
# can look for a string anywhere in a line as our selection criteria
4+
5+
fhand = open('mbox-short.txt')
6+
for line in fhand:
7+
line = line.rstrip()
8+
if not '@uct.ac.za' in line:
9+
continue
10+
print(line)
11+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# for assignment:
2+
3+
# [x] Open the file romeo.txt and read it line by line.
4+
# [x] For each line, split the line into a list of words using the split() method.
5+
# [] The program should build a list of words.
6+
# [x] For each word on each line check to see
7+
# if the word is already in the list and
8+
# [x] if not append it to the list.
9+
# [x] When the program completes, sort and print the resulting words
10+
11+
12+
new_list = []
13+
fname = input('Enter file name to open: ')
14+
fhand = open(fname)
15+
16+
for line in fhand:
17+
words = line.split() #separate words into items in list
18+
for word in words:
19+
if word in new_list:
20+
continue # account for (ignore) duplicate words
21+
new_list.append(word) # adds new words to list
22+
print(sorted(new_list)) # sorts and prints the final list
23+
# of unique words in the text

0 commit comments

Comments
 (0)