[Tutor] Problem with file()
Bernard Lebel
python at bernardlebel.com
Sat Aug 21 13:58:03 CEST 2004
Hello,
I'm writing a little module that has few classes, and each classes few
functions. The first class has a function that allows to read a text file
and add the entries of the text to a list, and the list is returned.
When I do the operation with command line in PythonWin, without the class
and function definitions, no problem. When I import the module and run the
function, I get an error:
oExtList = file( sListPath, 'r' )
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python23\lib\ClientsManager\__init__.py", line 104, in getextlist
oExtList = file( sListPath, 'r' )
TypeError: this constructor takes no arguments
I call the function this way:
import ClientsManager as cm
cl = cm.client()
aClients = cl.getextlist()
Duh?
Here is the full code:
class client:
def getextlist( self, aClients = [], sListPath = '', iOp = 2 ):
"""
Will modify an existing client list using an external file list.
You can choose to add to the list or remove from the list the clients
listed in the external list.
For instances, user computers on a floor generally give a non-standard
name to their computer, so you might find convenient to add/remove their
names
to the aClients list by querying an external file with the list of
computer names.
iOp specifies the operation to perform:
0: append
1: remove
"""
if sListPath == '':
sListPath = raw_input( 'Full path to external list (use "\\\\" to
separate directories): ' )
if iOp < 0 or iOp > 1:
iOp = int( raw_input( 'Type of operation (0-append, 1-remove): ' ) )
if iOp < 0 or iOp > 1:
print ( 'You must enter 0 or 1!' )
else:
if not os.path.exists( sListPath ):
print 'Provided list path invalid'
else:
# Get list file
oExtList = file( sListPath )
# Get list elements
aExtList = oExtList.readlines()
# Remove end of lines characters
for sClient1 in aExtList:
sClient2 = sClient1.strip( '\n' )
if iOp == 0:
aClients.append( sClient2 )
else:
aClients.remove( sClient2 )
return aClients
Thanks
Bernard
More information about the Tutor
mailing list