-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAndReadFiles.py
More file actions
39 lines (35 loc) · 1 KB
/
OpenAndReadFiles.py
File metadata and controls
39 lines (35 loc) · 1 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
# Open File, if doesnt exist, will fail, is a sequence lines
file = open('read.txt')
print(file)
# Read File
count = 0
for line in file:
count = count + 1
print('Line Count:', count)
inp = file.read()
print(inp[:20])
# Searching through a file
for line in file:
line = line.rstrip()
if line.startswith('Vittoria'):
print(line)
if not line.startswith('Vittoria'):
continue
print(line)
# User choose the file name
fname = input('Enter the file name: ')
try:
fhand = open(fname)
except:
print('File cannot be opened:', fname)
exit()
count = 0
for line in fhand:
if line.startswith('Subject:'):
count = count + 1
print('There were', count, 'subject lines in', fname)
# 7.1 Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below.
fname = input("Enter file name: ")
fh = open(fname)
inp = fh.read()
print(inp.upper().strip())