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 8453
On Thu, 11 Sep 2003 23:07:38 +0100, Graham Nicholls
<gr****@rockcon s.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****@rockcon s.co.uk> wrote in message
news:Km******** *******@news-binary.blueyond er.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****@rockcon s.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@A cmOrg
Gonçalo Rodrigues wrote: On Thu, 11 Sep 2003 23:07:38 +0100, Graham Nicholls <gr****@rockcon s.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)
This should return something like:
|
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 WORDS_PER_LINE = 4;
when counter == 7 is when the string Concatenation fails within the IF
block.
|
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' ).
I know that this can be achieved using strtok. But I was wondering
|
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 dynamic array of characters
int length; //number of characters
} String;
|
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 linked lists as follows:
(a) A double linked list called NAMES which will contain all C like
identifiers of less than
256 characters long identified in the input file F. Each identifier
| |
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 manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties, the XML file created by
serialization has three...
|
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 it. It reads in
the file and sorts out the files into the four different lists.
F.txt
int main
2
" "
help
|
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 simple SQL server table and stored
procedure which I have no problems binding from directly from an
ASP.NET control...
Table BOOK
|
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 std::list to store this information by inserting objects
containing all 3 pieces of information. However now some users has datasets
in the range of 12000 records and my list seems to be taking like forever to
complete inserting and finding.
|
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 {
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |