Okay so when I open a file and I would like to pickle something to the file you are supposed to open the file in 'rb' (read binary) mode to load stuff from it. If you want to dump something the file is supposed to be in 'wb' (write binary) mode. What if I would like to change the file's privileges after i have already initialized the variable? For example:

file = open('textfile.txt', 'wb')
pickle.dump(object, file)

# do i need to change the file privileges here in order to load?
# something like file.changePrivileges('rb')

newThing = pickle.load(file)

sorry if this is a trivial question and I haven't scanned the documentation enough to find it yet but I figured it'd be faster to ask then continue reading the documentation. Thanks for help in advance!

What if I would like to change the file's privileges after i have already initialized the variable?

You basically have two options:

1) Close the file handle and reopen (you can use the file handles member name if you don't want to hard code it):

>>> fh = open('some.file', 'wb')
>>> fh.write('Some text\n')
>>> fh.close()
>>> fh = open(fh.name, 'rb')
>>> fh.read()
'Some text\n'
>>> fh.close()
>>>

2) Open the file in 'read plus write' mode r+ . This probably isn't your best bet however as you'll need to constantly seek and tell around your file and it's probably more complication than you're looking for.

I think your best bet is to just close it when you're done, and reopen in the mode as needed.

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.