Really, really annoying problem
Fredrik Lundh
effbot at telia.com
Wed Sep 20 15:31:40 EDT 2000
Brett wrote:
> def addroot(text):
> # Definitions
> new_string = ""
> arg = sys.argv[0]
> split_arg = string.split(arg, '\\')
> # Cycle through
> for count in range(0, len(split_arg) - 1):
> new_string = new_string + split_arg[count] + "\\" # THIS IS
> THE PROBLEM
> new_string = new_string + text
> # Return the reconstructed string
> return new_string
>
> The function of this code is to append a filename to the directory the
> module is in, I'll add more functionality later, but for the minute this
> will do.
>
> Anyway, it does exactly what i want, but unexplainably, instead of inserting
> the one "\" like it should, it inserts "\\" .
are you sure? if you checked this from the command line,
you've tricked by python's
>>> a = "\\"
>>> a
'\\'
>>> print a
\
>>> len(a)
1
(in other words, if you just type a variable name at the prompt,
it's converted back to a string literal).
also, check out the functions in the os.path module:
http://www.python.org/doc/current/lib/module-os.path.html
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list