473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 8435
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
9
by: zacks | last post by:
I have written a serialized class that has several properties that are typed as a list of type class. When I deserialize an XML file, the list is populated just fine. But I am having trouble...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.