I'm trying to write a program which has a function that finds and prints the author of a file by looking for the Author
string in the docstring. I've managed to get the code below to print the author of a file that has the author string followed by the authors name and also the author string not followed by a name. The thing I'm having problems with is trying to print Unknown
when the author string does not exist at all i.e. no part of the docstring contains Author
.
N.B. lines
is just a list constructed by using readlines()
on a file.
def author_name(lines):
'''Finds the authors name within the docstring'''
for line in lines:
if line.startswith("Author"):
line = line.strip('\n')
line = line.strip('\'')
author_line = line.split(': ')
if len(author_line[1]) >=4:
print("{0:21}{1}".format("Author", author_line[1]))
else:
print("{0:21}{1}".format("Author", "Unknown"))