It's hard to be certain from your question as it stands, but I bet your problem is backslashes.
[EDITED to add:] Or actually maybe it's something simpler. Did you put quotes around your pathname at all? If not, that will certainly not work -- but once you do, you will find that then you need the rest of what I've written below.
In a Windows filesystem, the backslash
is the standard way to separate directories.
In a Python string literal, the backslash
is used for putting things into the string that would otherwise be difficult to enter. For instance, if you are writing a single-quoted string and you want a single quote in it, you can do this: 'don't'
. Or if you want a newline character, you can do this: 'First line.
Second line.'
So if you take a Windows pathname and plug it into Python like this:
os.startfile('C:fooaraz')
then the string actually passed to os.startfile
will not contain those backslashes; it will contain a form-feed character (from the f
) and two backspace characters (from the
s), which is not what you want at all.
You can deal with this in three ways.
You can use forward slashes instead of backslashes. Although Windows prefers backslashes in its user interface, forward slashes work too, and they don't have special meaning in Python string literals.
You can "escape" the backslashes: two backslashes in a row mean an actual backslash. os.startfile('C:\foo\bar\baz')
You can use a "raw string literal". Put an r
before the opening single or double quotes. This will make backslashes not get interpreted specially. os.startfile(r'C:fooaraz')
The last is maybe the nicest, except for one annoying quirk: backslash-quote is still special in a raw string literal so that you can still say 'don't'
, which means you can't end a raw string literal with a backslash.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…