472,353 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 8383
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) ...
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...
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...
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...
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....
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.