-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram_7_Find_Keywords.py
More file actions
39 lines (31 loc) · 1.32 KB
/
program_7_Find_Keywords.py
File metadata and controls
39 lines (31 loc) · 1.32 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
# keywords define the Python language
keywordList = [
'False', 'class', 'finally', 'is', 'return',
'None', 'continue', 'for', 'lambda', 'try',
'True', 'def', 'from', 'nonlocal', 'while',
'and', 'del', 'global', 'not', 'with',
'as', 'elif', 'if', 'or', 'yield',
'assert', 'else', 'import', 'pass',
'break', 'except', 'in', 'raise' ]
# Scan a text file for Python keywords
def main():
fileName = input('Enter the name of a python file: ')
# open the file
pyFile = open( fileName )
# get a list of strings that hold the file contents
# we call the pyFile object function 'readlines()'
pyFileLineList = pyFile.readlines()
# print some info about the file
print("The file", fileName, "has", len(pyFileLineList), "lines.")
# print the first 3 lines
print("The first three lines are:")
for line in pyFileLineList[0:3] :
print(line)
# lets see if we can find any keywords in the file
for line in pyFileLineList :
for keyword in keywordList:
if line.find( keyword + ' ' ) > -1 :
print("Found keyword:", keyword)
# This tells Python to run the function called main()
if __name__ == "__main__":
main()