39.3k views
4 votes
Suppose the Bookstore is processing an input file containing the titles of books in order to remove duplicates from their list. Write a program that reads all of the titles from an input file called bookTitles.txt and writes them to an output file called noDuplicates.txt. When complete, the output files should contain all unique titles found in the input file.

User Darwyn
by
4.9k points

1 Answer

2 votes

Answer:

books = []

fp = open("bookTitles.txt")

for line in fp.readlines():

title = line.strip()

if title not in books:

books.append(title)

fp.close()

fout = open("noDuplicates.txt", "w")

for title in books:

print(tile, file=fout)

fout.close()

except FileNotFoundError:

print("Unable to open bookTitles.txt")

User Sytech
by
4.5k points