如何使用python在其默认程序中打开文件 [英] How to open a file in its default program with python

查看:285
本文介绍了如何使用python在其默认程序中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python 3.5的默认应用程序中打开文件,特别是在记事本中打开"screen.txt".

I want to open a file in python 3.5 in its default application, specifically 'screen.txt' in Notepad.

我已经搜索了互联网,并在大多数答案中找到了os.startfile(path).我用文件的路径os.startfile(C:\[directories n stuff]\screen.txt)尝试了此操作,但返回了一条错误消息,提示行继续字符后出现意外字符".我尝试了没有文件路径的情况,只是文件名,但仍然无法正常工作.

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.

请提供打开有效的.txt文件的解决方案.

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

我使用的是Windows 7(受限制的(学校)计算机).

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

推荐答案

很难从您的问题上确定当前的状况,但是我敢打赌您的问题是反斜杠.

It's hard to be certain from your question as it stands, but I bet your problem is backslashes.

或者实际上也许更简单一些.您是否在路径名两边加上了引号?如果没有,那肯定是行不通的-但是一旦您这样做,您就会发现您需要我在下面写的其余内容.

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.

在Windows文件系统中,反斜杠\是分隔目录的标准方法.

In a Windows filesystem, the backslash \ is the standard way to separate directories.

在Python字符串文字中,反斜杠\用于将内容放入否则很难输入的字符串中.例如,如果您正在编写单引号字符串,并且想要在其中使用单引号,则可以执行以下操作:'don\'t'.或者,如果您想要换行符,可以执行以下操作:'First line.\nSecond line.'

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.'

因此,如果您采用Windows路径名并将其插入Python,如下所示:

So if you take a Windows pathname and plug it into Python like this:

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

然后实际传递给os.startfile的字符串将不包含那些反斜杠;它将包含一个换页字符(来自\f)和两个退格字符(来自\b s),这根本不是您想要的.

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.

  • 您可以使用正斜杠代替反斜杠.尽管Windows在其用户界面中更喜欢反斜杠,但是正斜杠也可以工作,并且它们在Python字符串文字中没有特殊含义.

  • 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.

您可以转义"反斜杠:连续两个反斜杠表示实际的反斜杠. os.startfile('C:\\foo\\bar\\baz')

You can "escape" the backslashes: two backslashes in a row mean an actual backslash. os.startfile('C:\\foo\\bar\\baz')

您可以使用原始字符串文字".将r放在单引号或双引号之前.这将使反斜杠不会被特别解释. os.startfile(r'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')

最后一个也许是最好的,除了,这是一个令人讨厌的怪癖:反斜杠引号在原始字符串文字中仍然很特殊,因此您仍然可以说'don\'t',这意味着您不能 end 带有反斜杠的原始字符串文字.

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.

这篇关于如何使用python在其默认程序中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆