Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file in its default program with python

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(path) on most of the answers. I tried that with the file's path os.startfile(C:\[directories n stuff]\screen.txt) but it returned an error saying 'unexpected character after line continuation character'. I tried it without the file's path, just the file's name but it still didn't work.

What does this error mean? I have never seen it before.

Please provide a solution for opening a .txt file that works.

EDIT: I am on Windows 7 on a restricted (school) computer.

like image 498
adam Avatar asked Dec 14 '22 06:12

adam


2 Answers

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:

os.startfile('C:\foo\bar\baz')

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. 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:\foo\bar\baz')

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.

like image 144
Gareth McCaughan Avatar answered Dec 24 '22 13:12

Gareth McCaughan


The recommended way to open a file with the default program is os.startfile. You can do something a bit more manual using os.system or subprocess though:

os.system(r'start ' + path_to_file')

or

subprocess.Popen('{start} {path}'.format(
    start='start', path=path_to_file), shell=True)

Of course, this won't work cross-platform, but it might be enough for your use case.

like image 26
Mike Driscoll Avatar answered Dec 24 '22 13:12

Mike Driscoll