469,282 Members | 1,949 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

list to string

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
--
#include <wit>
gr****@rockcons.co.uk
Jul 18 '05 #1
5 8267
On Thu, 11 Sep 2003 23:07:38 +0100, Graham Nicholls
<gr****@rockcons.co.uk> wrote:
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

Jul 18 '05 #2
"Graham Nicholls" <gr****@rockcons.co.uk> wrote in message
news:Km***************@news-binary.blueyonder.co.uk...
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
Jul 18 '05 #3
Graham Nicholls <gr****@rockcons.co.uk> writes:
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
Jul 18 '05 #4
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@AcmOrg

Jul 18 '05 #5
Gonçalo Rodrigues wrote:
On Thu, 11 Sep 2003 23:07:38 +0100, Graham Nicholls
<gr****@rockcons.co.uk> wrote:
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
--
#include <wit>
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by blrmaani | last post: by
13 posts views Thread by na1paj | last post: by
1 post views Thread by Little | last post: by
9 posts views Thread by zacks | last post: by
5 posts views Thread by Little | last post: by
6 posts views Thread by Henrik Goldman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.