Howdy folks, me again. :)

I'm attempting to append to a previously pickled file using cPickle.dump(). But I keep getting the error:

Traceback (most recent call last):
File "C:\Python25\Python\UserAccess.py", line 24, in <module>
p.dump(tempadd, temp)
TypeError: argument must have 'write' attribute

I've opened the file using the 'a+' attribute, which in my understanding should allow Reading, Writing, and appendage. I would love to know where I'm going wrong here, seeing as I've been bugging Google for an hour now, and the Python Docs only confirm what I thought I knew.

I've only been "trying" to program for around a week now, so if it's something quite obvious, please.... be gentle. :)

#User Access for Password.py
import cPickle as p
import sys
temp = p.load(open('systemaccess.txt', 'a+'))

def EP(self):
    if self.lower() == 'exit':
        print 'Exiting Program.'
        sys.exit()
        
print 'Welcome to the System Account Setup.'
print 'Enter Exit to end the program at any time.'
Username = raw_input('Please enter the username you desire: ')
EP(Username.lower())
Password = raw_input('Enter your desired password: ')
EP(Password.lower())

if Username.lower() in temp:
    print 'That username already exists.'
else:
    tempadd = temp
    tempadd[Username.lower()] = Password.lower()
    print tempadd #Print to screen for debug. Remove in final revision.
    p.dump(tempadd, temp)
    temp.close()

Recommended Answers

All 2 Replies

you need to use the file handle and not the loaded object when you dump. You should just reopen the file for reading or writing and not worry about a+.

#User Access for Password.py
import cPickle as p
import sys

my_file = 'systemaccess.txt'
file_handle = open( my_file, 'r' )
temp = p.load( file_handle )
file_handle.close()

def EP(self):
    if self.lower() == 'exit':
        print 'Exiting Program.'
        sys.exit()
        
print 'Welcome to the System Account Setup.'
print 'Enter Exit to end the program at any time.'
Username = raw_input('Please enter the username you desire: ')
EP(Username.lower())
Password = raw_input('Enter your desired password: ')
EP(Password.lower())

if Username.lower() in temp:
    print 'That username already exists.'
else:
    temp[Username.lower()] = Password.lower()
    file_handle = open( my_file, 'w' )
    p.dump(temp, file_handle)
    file_handle.close()

Worked Perfectly, and thanks for the explanation. Hope I can keep this straight in my mind now. lol

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.