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 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
"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
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
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
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)
...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
| |