I want to open a file in python 3.5 in its default application, specifically 'screen.txt' in Notepad.
I have searched the internet, and found
os.startfile(C:\[directories n stuff]\screen.txt)
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.\nSecond line.'
So if you take a Windows pathname and plug it into Python like this:
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
\bs), 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.
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.
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.