我有点坚持zed shaw的练习15。实际上,我对原始程序没有问题,但问题是当我尝试额外的学分时,他要求我们使用原始输入而不是argv。
所以,这是我使用的代码
filename=raw_input("enter filename :")
print "here's your file %r" % filename
txt=open(filename)
print txt.read()当它要求输入文件名时,我给出了路径e:\python\ex15_sample.txt,在这一行我得到了以下错误--> txt =open(文件名),而且它还指出没有这样的文件或目录
那么,我该怎么办呢?
发布于 2012-04-19 17:07:59
由于您使用的是Windows,因此您可以尝试在输入文件名时使用/ (正斜杠),或者对路径分隔符\\使用双反斜杠。
要输入文件名,可以尝试使用e:/python/ex15_sample.txt或e:\\python\\ex15_sample.txt。
发布于 2012-04-19 17:00:27
你的代码很好。输入文件名时出错。检查该文件是否确实存在。
>>> filename=raw_input('enter filename :')
enter filename :c:\Users\All Users\Autodesk\Revit\Addins\2012\RevitLookup.addin
>>> txt = open(filename)
>>> print txt.read()
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<RevitAddIns>
<AddIn Type="Application">
<Assembly>C:\Program Files (x86)\Revit 2012 SDK\RevitLookup\CS\bin\Debug\RevitLookup.dll</Assembly>
<ClientId>356CDA5A-E6C5-4c2f-A9EF-B3222116B8C8</ClientId>
<FullClassName>RevitLookup.App</FullClassName>
<Name>Revit Lookup</Name>
<VendorId>ADSK</VendorId>
<VendorDescription>Autodesk, www.autodesk.com</VendorDescription>
</AddIn>
</RevitAddIns>(呃,那只是我电脑里碰巧有的一些文件……)
确保在输入文件时不要使用引号-或者在输入文件后将引号去掉!对argv这样做可能行得通,但对raw_input来说肯定不行。
编辑路径:我认为这就是问题所在:你输入的文件名带有引号(就像你在浏览器中按住shift右键单击"Copy as “时得到的一样)。对于sys.argv,这些是通过(Python?操作系统?我认为是Python...),但不是使用raw_input。
发布于 2016-12-05 09:59:12
我使用以下程序最终使其正常工作:
print "Type your filename:"
filename = raw_input(">")
txt = open(filename)
print txt.read()我有点不明白为什么OP使用%r格式化程序。我没有使用它,我的程序仍然有效。我是不是漏掉了什么?谢谢。
https://stackoverflow.com/questions/10224688
复制相似问题