list to string

Scott David Daniels Scott.Daniels at Acm.Org
Thu Sep 11 19:33:38 EDT 2003


Graham Nicholls wrote:

> Hi.  Sorry if this is really dim, but I'm trying to do something like:
> 
> main (args):
>         fhand=open(args[1:],"r+")
> 
> and get an error that open is expecting a string not a list.
> 
> I _have_ looked at the docs online, and in the books, but to no avail, so
> thanks for looking!
> 
> Graham 

If your program was called with:

	python myprog.py one two

Then sys.argv will be the list:
	['myprog.py' 'one' 'two']
and sys.argv[1:]
	['one' 'two']

To find out about this kind of thing, instrument your program as follows:

     main (args):
         print 'main called with args = %r' % args
         fhand=open(args[1:],"r+")

This should make it obvious what is going on.

-Scott David Daniels
Scott.Daniels at AcmOrg





More information about the Python-list mailing list