[Tutor] Problem with file()

Kent Johnson kent_johnson at skillsoft.com
Sat Aug 21 14:57:14 CEST 2004


Bernard,

You don't seem to have included the code containing the problem. The 
traceback shows the error occurring at line 104 of __init__.py and you seem 
to have included ClientsManager.py.

Have you perhaps redefined 'file' in __init__.py? It's easy to do...

BTW from what you have shown here, using a class to hold the getextlist() 
function may be overkill. Classes are great when you have several functions 
that have to work together, for example sharing common data in fields. If 
you have independent functions that you want to group together for 
convenience/modularity/testing/reuse, you can make them global functions in 
a separate module without creating a class.

Kent

At 12:58 PM 8/21/2004 +0100, Bernard Lebel wrote:
>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
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list