Writeing new files
Andy Todd
andy_todd at spam.free.yahoo.com
Thu Aug 30 02:34:55 EDT 2001
Steve challenged, I had to respond ;-)
"Steve Holden" <sholden at holdenweb.com> wrote in
<x0cj7.71700$54.3813230 at e420r-atl1.usenetserver.com>:
>"eber kain" <eberkain at earthlink.net> wrote in message
>news:b5f84cf8.0108291125.1fffe46b at posting.google.com...
>> When i run this code
>>
>> name= Create('Name')
>> file= open('%s.btv' % name, "w")
>>
>> i end up with a file name that has a set of ' ' around the name like
>> so
>>
>> 'Name'.btv
>>
>> How can i fix this?
>
>>>> name = Create('Name')
>Traceback (innermost last):
> File "<interactive input>", line 1, in ?
>NameError: There is no variable named 'Create'
>
>I think we need a little more context before we can be of much help,
>despite this newsgroup's famous psychic debugging abilities. That is,
>unless someone out there wants to prove me wrong ...
My lack of pyschic ability lead me to;
>>> filename1='''Name'''
>>> print '%s.btv' % filename1
Name.btv
So that obviously works ... hmmm
Then I tried;
>>> filename2="'Name'"
>>> print '%s.btv' % filename2
'Name'.btv
Bingo! This is more than likely causing your problem. Note the combination
of double and single quotes. They are guaranteed to trip up the unwary.
Obviously I skipped
>>> filename0='Name'
because I new that would give us;
>>> print '%s.btv' % filename0
Name.btv
The moral of the story? Try to avoid encoding quotation marks in you
character strings at all costs. If you do need to preserve them, be very,
very careful.
For what its worth my rule of thumb is;
Use ' for ordinary characters,
Use " if you *must* include quotation marks within the string (then watch
out when evaluating these strings)
Oh, and test like buggery as well ;-)
If you want the total lowdown on string literals (for some reason I only
have the 2.0.1 reference but I don't believe it has changed), look in the
manual;
http://www.python.org/doc/2.0.1/ref/strings.html
>
>About all I can tell you is that you are putting quotes in somewhere
>they aren't required, but you'd probably wirked that out.
>
>Eber, please post the whole program, or if it's too long (say, > 100
>lines) or a secret then write a shorter program to demonstrate the bug
>and post that.
Here, here. Although, hopefully the miniscule examples above will lead you
to solve your problem.
>
>regards
> Steve
>--
>http://www.holdenweb.com/
>
Regards,
Andy
--
Content free posts a speciality
More information about the Python-list
mailing list