I am creating a page scraper and would like to include a text file that contains the words such as 'a', 'the', 'on' etc. But not sure how to do this, i think I have created the file

fin=open("ignorelist.txt","w")
for line in fin:

but not sure how to go about filling the file with these words found, any help would be great.

Recommended Answers

All 2 Replies

for statement iteration is only for reading, for writing it is recommended to use with statement for safe opening and closing of the file.

with open("ignorelist.txt", "w") as fout:
    fout.write('\n'.join(my_ignorelist))

This might help too ...

# words you want to ignore later
text = '''\
a
the
on
up
down
'''

fname = "ignorelist.txt" 
# write the file out
with open(fname, "w") as fout:
    fout.write(text)

# read the file back in and convert into a list
with open(fname, "r") as fin:
    ignorelist = [ word.strip() for word in fin]

print(ignorelist)

''' result ...
['a', 'the', 'on', 'up', 'down']
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.