使用百分比登录操作系统或者os.popen公司

2024-05-23 15:37:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用带有%的用户定义的字符串打开一个带有exe的文件,这对我来说是不起作用的。我试过了操作系统以及os.popen公司. 我一直得到这个
TypeError:不支持%的操作数类型:“file”和“str”

我该怎么做才能让它成功呢?在

def showhelp():
 defaulteditor = "notepad.exe"
 print "[*] Which text viewer do you want to use? [default: notepad]"
 which = raw_input("\n\n\ntype n for notepad, or specify program.exe  >     ")
 if which != "n":
    os.popen('notepad ./help.txt')
else:
    os.popen('%r')%(which)

Tags: 文件字符串用户类型which定义os公司
3条回答

如果您使用^{} module而不是不推荐的方式来进行调用,则可以避免此问题并拥有一个更干净的程序。在

也许这可以帮助你回答问题:

>>> def printy(string):
    print string


>>> printy("hello %s" % "world")
hello world
>>> printy("hello %s") % "world"
hello %s

Traceback (most recent call last):
  File "<pyshell#278>", line 1, in <module>
    printy("hello %s") % "world"
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'
>>> 

如您所见,您没有在正确的位置包含%运算符或其补充。在

你甚至可以参考这些文档:http://docs.python.org/release/2.4.4/lib/typesseq-strings.html

还包括在python附带的python帮助文档中。在

试试看

os.popen('%r' % which)

相反。在

%格式化运算符需要格式字符串在左侧,参数在右侧。另外,如果只有一个参数,则不需要括号。在

相关问题 更多 >