在记事本中启动文件 [英] Launch file in Notepad
问题描述
新手问题:
我正试图从Python中打开记事本来打开文本文件:
import os
b1 =" c:\ test.txt"
os.system(''notepad.exe''+ b1)
但是,测试t被\转义,导致记事本尝试打开c:\\ test.txt打开
。
如何我解决了这个问题吗?
(顺便说一句,b1来自命令行参数,因此用户输入
c:\ test.txt作为命令行参数。)
乔治
乔治在2005-05-12 09对世界说:41:新手问题:
我正在尝试用Python打开一个文本文件:
import os > b1 =" c:\test.txt"
os.system(''notepad.exe''+ b1)
然而,t的测试是通过\\逃脱的\\,导致Notepad tryi打开c:\\ test.txt。
如何解决这个问题?
有几种方法,但首选的解决方案是切换
斜线方向:" c:/test.txt"。 Python足够聪明,可以注意到它在Windows上运行的
并使用斜杠做正确的事情。 (其他
选择包括使用原始字符串:r" c:\ test.txt",并明确地
转义反斜杠:" c:\\ test .txt"。)
Best,
Brian vdB
< blockquote>
[George]
b1 =" c:\ test.txt"
使用此代码,您的问题就是您所说的嵌入式标签。使用
r" c:\ test.txt"或c:\\test.txt。但是,如果这是真的:
顺便说一句,b1来自命令行参数,因此用户输入
c:\ test.txt作为命令行参数。
那么就没有嵌入式标签了。你是如何提示用户的?当
我运行时:
import os
b1 = raw_input("输入文件名:")
os.system(''notepad.exe''+ b1)
并输入c:\ test.txt,它按预期工作。
[Brian]有几种方法,但首选的解决方案是切换
斜线方向:c:/test.txt"。 Python非常聪明,可以注意到它在Windows上运行并使用斜杠做正确的事情。
为了公平对待Windows,我不会''相信Python在这里做了什么特别的事。
Windows本身很高兴接受OS的正斜杠或反斜杠
级别 - 它只是shell(explorer.exe或者在较小程度上cmd.exe)
不接受正斜杠。
-
Richie Hindle
ri****@entrian.com
2005-05-12,Brian van den Broek< bv **** @ po-box.mcgill.ca>写道:
b1 =" c:\test.txt"
os.system(''notepad.exe''+ b1)
然而,测试结果被\转义,导致记事本尝试打开c:\\ test.txt。
有几种方法,但首选的解决方案是切换
斜线方向:c:/test.txt"。 Python足够聪明,可以注意到它在Windows上运行并使用斜杠做正确的事。
Python真的看着字符串和混乱斜线?
我不认为它需要,因为Windows系统调用了
总是接受正斜线,不是吗?
-
格兰特爱德华兹格兰特哇!在PHIL SILVERS和
visi.com ALEXANDER HAIG之间向我询问差价
Newbie question:
I''m trying to lauch Notepad from Python to open a textfile:
import os
b1="c:\test.txt"
os.system(''notepad.exe '' + b1)
However, the t of test is escaped by the \, resulting in Notepad trying
to open "c: est.txt".
How do I solve this?
(By the way, b1 comes from a command line parameter, so the user enters
c:\test.txt as command line parameter.)
George
George said unto the world upon 2005-05-12 09:41:Newbie question:
I''m trying to lauch Notepad from Python to open a textfile:
import os
b1="c:\test.txt"
os.system(''notepad.exe '' + b1)
However, the t of test is escaped by the \, resulting in Notepad trying
to open "c: est.txt".
How do I solve this?
There are several ways, but the preferred solution is to switch the
slash direction: "c:/test.txt". Python''s smart enough to notice its
running on Windows and do the right thing with the slash. (Other
choice include using raw strings: r"c:\test.txt", and explicitly
escaping the backslash: "c:\\test.txt".)
Best,
Brian vdB
[George]b1="c:\test.txt"
With this code, your problem is the embedded tab as you say. Use either
r"c:\test.txt" or "c:\\test.txt". However, if this is true:
By the way, b1 comes from a command line parameter, so the user enters
c:\test.txt as command line parameter.
then there will be no embedded tab. How are you prompting the user? When
I run this:
import os
b1=raw_input("Enter a filename: ")
os.system(''notepad.exe '' + b1)
and enter c:\test.txt, it works as expected.
[Brian] There are several ways, but the preferred solution is to switch the
slash direction: "c:/test.txt". Python''s smart enough to notice its
running on Windows and do the right thing with the slash.
In fairness to Windows, I don''t believe Python does anything special here.
Windows itself happily accepts either forward slash or backslash at the OS
level - it''s only the shells (explorer.exe or to a lesser extent cmd.exe)
that don''t accept forward slashes.
--
Richie Hindle
ri****@entrian.com
On 2005-05-12, Brian van den Broek <bv****@po-box.mcgill.ca> wrote:
b1="c:\test.txt"
os.system(''notepad.exe '' + b1)
However, the t of test is escaped by the \, resulting in Notepad trying
to open "c: est.txt".There are several ways, but the preferred solution is to switch the
slash direction: "c:/test.txt". Python''s smart enough to notice its
running on Windows and do the right thing with the slash.
Does Python really look at the string and mess with the slash?
I don''t think it needs to, since the Windows system calls have
always accepted forward slashses, haven''t they?
--
Grant Edwards grante Yow! Ask me the DIFFERENCE
at between PHIL SILVERS and
visi.com ALEXANDER HAIG!!
这篇关于在记事本中启动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!