简体   繁体   English

如何使用python在其默认程序中打开文件

[英]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. 我想在python 3.5的默认应用程序中打开文件,特别是在记事本中打开“ screen.txt”。

I have searched the internet, and found os.startfile(path) on most of the answers. 我已经搜索了互联网,并在大多数答案中找到了os.startfile(path) 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'. 我用文件路径os.startfile(C:\\[directories n stuff]\\screen.txt)尝试了此操作,但是它返回一个错误,提示“行继续符后出现意外字符”。 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. 请提供打开有效的.txt文件的解决方案。

EDIT: I am on Windows 7 on a restricted (school) computer. 编辑:我在受限(学校)计算机上的Windows 7上。

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. 在Windows文件系统中,反斜杠\\是分隔目录的标准方法。

In a Python string literal, the backslash \\ is used for putting things into the string that would otherwise be difficult to enter. 在Python字符串文字中,反斜杠\\用于将东西放入否则很难输入的字符串中。 For instance, if you are writing a single-quoted string and you want a single quote in it, you can do this: 'don\\'t' . 例如,如果您正在编写单引号字符串,并且希望在其中使用单引号,则可以执行以下操作: 'don\\'t' Or if you want a newline character, you can do this: 'First line.\\nSecond line.' 或者,如果您想要换行符,则可以执行以下操作: 'First line.\\nSecond line.'

So if you take a Windows pathname and plug it into Python like this: 因此,如果您采用Windows路径名并将其插入Python,如下所示:

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

then the string actually passed to os.startfile will not contain those backslashes; 那么实际传递给os.startfile的字符串将不包含那些反斜杠; it will contain a form-feed character (from the \\f ) and two backspace characters (from the \\b s), which is not what you want at all. 它将包含一个换页字符(来自\\f )和两个退格字符(来自\\b ),这根本不是您想要的。

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. 尽管Windows在其用户界面中更喜欢反斜杠,但是正斜杠也可以工作,并且它们在Python字符串文字中没有特殊含义。

  • 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. 在单引号或双引号前加上r 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. 最后一个也许是最好的, 除了一个令人讨厌的怪癖:反斜杠引号在原始字符串文字中仍然很特殊,因此您仍然可以说'don\\'t' ,这意味着您不能用a 结束原始字符串文字。反斜杠。

The recommended way to open a file with the default program is os.startfile . 建议使用默认程序打开文件的方式是os.startfile You can do something a bit more manual using os.system or subprocess though: 不过,您可以使用os.systemsubprocess来做一些手动操作:

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. 当然,这不能跨平台工作,但是对于您的用例来说可能就足够了。

For example I created file "test file.txt" on my drive D: so file path is 'D:/test file.txt' Now I can open it with associated program with that script: 例如,我在驱动器D:上创建了文件"test file.txt" ,因此文件路径为'D:/test file.txt'现在,我可以使用该脚本通过关联的程序打开它:

import os
os.startfile('d:/test file.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM