Re: problems with opening files due to file's path
- From: Lie <Lie.1296@xxxxxxxxx>
- Date: Wed, 11 Jun 2008 06:48:45 -0700 (PDT)
On Jun 11, 10:07 am, Alexnb <alexnbr...@xxxxxxxxx> wrote:
I don't think you understand it doesn't matter how the variable gets there,
the same code is run regardless, I have no problem with the GUI, but you
asked, and so I told you. the code os.startfile(.... is run if there is a
GUI or it is a console app.
(snip)
I think I know why you get confused by this, clearly, you have no idea
of what an escape character and escape sequence is.
Python (and C/C++ and some other languages) treats \ (the backspace
character) inside a string specially, it makes the character after the
backspace lose their special meaning OR get a special meaning, you
might probably be used to seeing something like this: 'First line
\nSecond Line', which when printed, would give:
First Lineprint 'First Line\nSecond Line'
Second Line
The second behavior of the escape sequence is to make special
character (generally the backspace itself), lose their special
meaning:
path\file.txtprint 'path\\file.txt'
In some cases, you might sometimes want a path like this: 'path
\nick.txt'
if you do this:
pathprint 'path\nick.txt'
ick.txt
because the \n is considered as a newline.
Instead, you should do this:
path\nick.txtprint 'path\\nick.txt'
or
path\nick.txtprint r'path\nick.txt'
the r'' string is raw string, most of the magics of a regular string
'' is lost for an r'' string. It allows you to avoid the need to
escape the special characters. Raw string is usually used for re
(regular expressions) and paths in Windows both of which uses the
backslash character regularly.
you first case of:
system("\"C:\Documents and Settings\Alex\My Documents\My Music\Rhapsody
\Bryanbros\Weezer\(2001)\04 - Island In The Sun.wma\"")
is interpreted by python as this:
"C:\Documents and Settings\Alex\My Documents\My Music\Rhapsody
\Bryanbros\Weezer\(2001) - Island In The Sun.wma"
Notice that the '\0' is substituted into '', because \0 is the escape
sequence for null character.
(Personally I think python should raise errors if any escape character
that isn't followed by a valid escape sequence is found (such as \D,
\A, \M, \M, \R, \B, \W, \(), but perhaps they're trying not to be too
mean for newbies.)
that should be correctly written like this:
system(r'"C:\Documents and Settings\Alex\My Documents\My Music\Rhapsody
\Bryanbros\Weezer\(2001)\04 - Island In The Sun.wma"')
or:
system('"C:\\Documents and Settings\\Alex\\My Documents\\My Music\
\Rhapsody\\Bryanbros\\Weezer\\(2001)\\04 - Island In The Sun.wma"')
Now, to the next question:
How if I want the path to come from a variable:
path = "C:\Documents and Settings\Alex\My Documents\My Music\Rhapsody
\Bryanbros\Jason Mraz\I'm Yours (Single)\01 - I'm Yours.wma"
os.startfile(path)
I can see in a glance why it doesn't work, the string is escaped by
python, it should be written like this.
path = r"C:\Documents and Settings\Alex\My Documents\My Music\Rhapsody
\Bryanbros\Jason Mraz\I'm Yours (Single)\01 - I'm Yours.wma"
os.startfile(path)
OR:
path = "C:\\Documents and Settings\\Alex\\My Documents\\My Music\
\Rhapsody\\Bryanbros\\Jason Mraz\\I'm Yours (Single)\\01 - I'm
Yours.wma"
os.startfile(path)
In most GUI toolkits (including Tkinter) and raw_input() function,
when you input a string (using the textbox, a.k.a Entry widget) it
would automatically be escaped for you, so when you input 'path\path
\file.txt', the GUI toolkit would convert it into 'path\\path\
\file.txt'.
.
- Follow-Ups:
- Re: problems with opening files due to file's path
- From: Carsten Haese
- Re: problems with opening files due to file's path
- References:
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Grant Edwards
- Re: problems with opening files due to file's path
- From: Carsten Haese
- Re: problems with opening files due to file's path
- From: Alexnb
- Re: problems with opening files due to file's path
- Prev by Date: Re: Producer-consumer threading problem
- Next by Date: Re: Producer-consumer threading problem
- Previous by thread: Re: problems with opening files due to file's path
- Next by thread: Re: problems with opening files due to file's path
- Index(es):
Relevant Pages
|
|