problems with opening files due to file's path

Grant Edwards grante at visi.com
Wed Jun 11 04:43:54 CEST 2008


On 2008-06-11, Alexnb <alexnbryan at gmail.com> wrote:

>>>> path = "C:\Documents and Settings\Alex\My Documents\My
>>>> Music\Rhapsody\Bryanbros\Jason Mraz\I'm Yours (Single)\01 - I'm
>>>> Yours.wma"

Your string doesn't contain what you think it does.  Do a
"print path".  Hint: the string "\01" consists of a single byte
who's value is 001 base 8.

>>>> os.startfile(path)
> Traceback (most recent call last):
>   File "<pyshell#39>", line 1, in <module>
>     os.startfile(path)
> WindowsError: [Error 2] The system cannot find the file specified:
> "C:\\Documents and Settings\\Alex\\My Documents\\My
> Music\\Rhapsody\\Bryanbros\\Jason Mraz\\I'm Yours (Single)\x01 - I'm
> Yours.wma"

Notice that the string in the error message contains \x01?
That's the byte that got changed.

> Here's another way:
>
>>>> os.startfile(r"%s"%path)
> Traceback (most recent call last):
>   File "<pyshell#40>", line 1, in <module>
>     os.startfile(r"%s"%path)
> WindowsError: [Error 2] The system cannot find the file specified:
> "C:\\Documents and Settings\\Alex\\My Documents\\My
> Music\\Rhapsody\\Bryanbros\\Jason Mraz\\I'm Yours (Single)\x01 - I'm
> Yours.wma"
>
> Same output, however if I personally input it like so:
>
>>>> os.startfile("C:\\Documents and Settings\\Alex\\My Documents\\My
>>>> Music\\Rhapsody\\Bryanbros\\Jason Mraz\\I'm Yours (Single)\\01 - I'm
>>>> Yours.wma")
>
> It works out fine because I can make each backslash doubles so it doesn't
> mess stuff up.

Right.

> So if I could take the path varible and make ever "\" into a
> "\\" then it would also work. 

I don't understand the part about the path variable.

The backslash character is used in both C and Python as an
escape character so that you can encode special values in
string literals.  For example: '\r' is a carriage return '\n'
is a linefeed, \0nnn is a single byte with the octal value nnn,
and so on.

Microsoft's choice of '\' as a path separator was a terribly
bad one (one of many made by Microsoft over the years).  Most
Windows system calls will accept forward slashes, so the
easiest thing to do is usually just type the strings with
forward slashes.

-- 
Grant Edwards                   grante             Yow!  NOW do I get to blow
                                  at               out the CANLDES??
                               visi.com            



More information about the Python-list mailing list