Windows file paths, again

Anthony Tolle anthony.tolle at gmail.com
Wed Oct 21 15:50:02 EDT 2009


On Oct 21, 3:20 pm, Dan Guido <dgu... at gmail.com> wrote:
> Hi Diez,
>
> The source of the string literals is ConfigParser, so I can't just
> mark them with an 'r'.
>
> config = ConfigParser.RawConfigParser()
> config.read(filename)
> crazyfilepath = config.get(name, "ImagePath")
> normalfilepath = normalize_path(crazyfilepath)
>
> The ultimate origin of the strings is the _winreg function. Here I
> also can't mark them with an 'r'.
>
> regkey = OpenKey(HKEY_LOCAL_MACHINE,
> "SYSTEM\\CurrentControlSet\\Services\\" + name)
> crazyimagepath = QueryValueEx(regkey, "ImagePath")[0]
> CloseKey(key)
>
> --
> Dan Guido
>

I just did a quick test using Python 2.5.1 with the following script
on Windows:

# start of test.py
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read("cfg.ini")
x = config.get("foo", "bar")
print x
print repr(x)
from _winreg import *
regkey = OpenKey(HKEY_LOCAL_MACHINE,
r"SYSTEM\CurrentControlSet\Services\IPSec")
x = QueryValueEx(regkey, "ImagePath")[0]
CloseKey(regkey)
print x
print repr(x)
# end of test.py


Here is the contesnts of cfg.ini:

[foo]
bar=c:\dir\file.txt


Here is the output of the script:

c:\dir\file.txt
'c:\\dir\\file.txt'
system32\DRIVERS\ipsec.sys
u'system32\\DRIVERS\\ipsec.sys'


In either case, I don't see the functions returning strings that
requires special handling.  The backslashes are properly escaped in
the repr of both strings.

Something else must be going on if the strings are getting messed up
along the way.



More information about the Python-list mailing list