Really, really annoying problem

Fredrik Lundh effbot at telia.com
Wed Sep 20 16:01:38 EDT 2000


Brett wrote:
> > >>> a = "\\"
> > >>> a
> > '\\'
> > >>> print a
> > \
> 
> I think that might be the problem, i'm using this method to get the Filename
> for a config file (in your example >>> a), so what i want might be
> 
> "C:\Program Files\My Python App\Lib\Config\ppdump.txt"
> 
> but what Python's giving me is:
> 
> "C:\\Program Files\\My Python App\\Lib\\Config\\ppdump.txt"
> 
> which returns an error when i try to open the file for writing, because it's
> an invalid path.

Sorry, my post wasn't quite finished when it left my
computer.  Let's try again:

:::

First, your script doesn't add any extra backslashes on
my box.  The function is fine (it can be written in fewer
lines, but that's not important here).

I'm pretty sure you're being tricked by Python's command
line interface.  Let's look at my example again:

The "a" variable is initialized to 1-character string that
contains a single backslash.

Now, if I just type "a" at the command line prompt, Python
runs it through the "repr" function before printing it. This
function adds quotes, and converts backslashes and non-
printable characters to their literal form.

On the other hand, if I use "print", Python prints the
actual content: a single backslash.

:::

So to figure out where your problem really is, we need some
more info:

- how do you read in the filename?

- how do you know that the backslashes are doubled?
  (in other words, how did you print the filename?)

</F>




More information about the Python-list mailing list