Skip to content

Commit c190bea

Browse files
committed
5 March 2021
1 parent ca30fcb commit c190bea

File tree

10 files changed

+67
-0
lines changed

10 files changed

+67
-0
lines changed

File/BadFileNames.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fname = input('Enter the file name:')
2+
try:
3+
fhand = open(fname)
4+
except:
5+
print('File cannot be opened:', fname)
6+
quit()
7+
8+
count = 0
9+
for line in fhand:
10+
if line.startswith('Subject:'):
11+
count = count + 1
12+
print('There were' , count, 'subject lines in', fname)

File/PromptFile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fname = input('Enter the file name: ')
2+
fhand = open(fname)
3+
count = 0
4+
for line in fhand:
5+
if line.startswith('Subject:'):
6+
count = count + 1
7+
print('There were' , count, 'subject lines in' , fname)

File/SearchingThroughAFile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fhand = open('a.txt')
2+
for line in fhand:
3+
if line.startswith('From'):
4+
print (line)

File/SelectLines.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# We can look for a string anywhere in a line as our selection criteria
2+
3+
fhand = open('a.txt')
4+
for line in fhand:
5+
line = line.rstrip()
6+
if not '@uct.ac.za' in line:
7+
continue
8+
print (line)

File/a.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
It will work as the first line
2+
It will work as the second line
3+
It will work as the third line
4+
It will work as the forth line
5+
It will work as the fifth line

File/countLine.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fhand = open('a.txt')
2+
count = 0
3+
4+
for i in fhand:
5+
count += 1
6+
print('Line Count:' , count)

File/searching1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Getting output without the whitespaces
2+
3+
fhand = open('a.txt')
4+
for line in fhand:
5+
line = line.rstrip()
6+
if line.startswith('From:'):
7+
print(line)
8+
9+
# Skipping with continue
10+
11+
fhand = open('a.txt')
12+
for line in fhand:
13+
line = line.rstrip()
14+
if not line.startswith('From:'):
15+
continue
16+
print(line)

File/test.txt

Whitespace-only changes.

File/trial.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fhand = open('test.txt')
2+
print(fhand)
3+
4+
# Output
5+
# <_io.TextIOWrapper name='test.txt' mode='r' encoding='cp1252'>

File/trial1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
xfile = open('a.txt')
2+
3+
for i in xfile:
4+
print(i)

0 commit comments

Comments
 (0)