list to string

G

Graham Nicholls

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
 
G

Gonçalo Rodrigues

Hi. Sorry if this is really dim, but I'm trying to do something like:

main (args):
fhand=open(args[1:],"r+")

args[1:] is a slice. So if args is a list, args[1:] is a list, if it's
a string, it's a string, etc... open expects a string -- as you
probably have already figured out.
and get an error that open is expecting a string not a list.

So either you are passing the wrong argument: e.g. a list instead of a
string, or then, if you do want to pass a list then you have to change
your code. But now the course to follow depends on what you want to
do. So you have to explain a little more.

With my best regards,
G. Rodrigues
 
S

Sean Ross

Graham Nicholls said:
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.

args[1:] performs a slicing operation which returns a list.
You're asking for all of the elements in the list 'args' except for the very
first (arg[0]). If you only want the second argument, use arg[1]. If you
want the slice, but as a string, use ''.join(args[1:]) (or whatever
seperator you require).

HTH
Sean
 
J

John J. Lee

Graham Nicholls said:
main (args):
fhand=open(args[1:],"r+")

and get an error that open is expecting a string not a list.
[...]

If args is sys.argv, then yup, that's a list. print is your friend:

main (args):
print args[1:]
sys.exit()
fhand=open(args[1:],"r+")


John
 
S

Scott David Daniels

Graham said:
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@AcmOrg
 
G

Graham Nicholls

Gonçalo Rodrigues said:
Hi. Sorry if this is really dim, but I'm trying to do something like:

main (args):
fhand=open(args[1:],"r+")

args[1:] is a slice. So if args is a list, args[1:] is a list, if it's
a string, it's a string, etc... open expects a string -- as you
probably have already figured out.
and get an error that open is expecting a string not a list.

So either you are passing the wrong argument: e.g. a list instead of a
string, or then, if you do want to pass a list then you have to change
your code. But now the course to follow depends on what you want to
do. So you have to explain a little more.

With my best regards,
G. Rodrigues
Thanks all - I knew it was something stupid. These days I don't get to do
enough programming, so I tend to make silly mistakes like this one -
clearly I wanted something like:
fhand=open(argv[1],"rw")

So thanks again.

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,435
Latest member
PhilipBelm

Latest Threads

Top