user3105664
user3105664

Reputation: 179

Writing a list to a file using Pickle

I'm relatively new to Python, and I have a Shakespeare poem as a list that I want to write into a folder on my computer and save, but be able to open it again later on.

get_text(page_1) = [Over hill, over dale, Thorough bush, thorough brier, Over park, over pale, Thorough flood, thorough fire! I do wander everywhere, Swifter than the moon's sphere; And I serve the Fairy Queen, To dew her orbs upon the green; The cowslips tall her pensioners be; In their gold coats spots you see; Those be rubies, fairy favours; In those freckles live their savours; I must go seek some dewdrops here, And hang a pearl in every cowslip's ear.]

Here's what I have:

import pickle 

pickle.dump(get_text(page_1),'C:\Users\Matt\Honors Poems')

How should I fix this so that it'll be able to write into a file on my computer?

Upvotes: 0

Views: 108

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122252

The second argument has to be an open file object:

with open(r'C:\Users\Matt\Honors Poems', 'wb') as outfile:
    pickle.dump(get_text(page_1), outfile)

Upvotes: 2

Related Questions