2009-12-31 57 views
1

这是为什么发生?为什么意外缩进?

def LoadPackageList(): 
    try: 
     #Attempts to load package list... Adds each neccessary attribute into array 
     print("Loading Package List... please wait") 
     packages = [] 
     packagelisturl = os.getcwd() + "packages.list" 
     dom = minidom.parse(urllib.urlopen(packagelisturl)) 
     try: 
      for eachattributeofpkglist in dom.GetElementsByTagNameNS(packagelist, 'packages'): 
       packages.append({ 
        'title': node.getAttribute('title'), 
        'shortname': node.getAttribute('shortname'), 
        'dlurl': node.getAttributes('dlurl'), 
        'description': node.getAttributes('description'), 
        'tags': node.getAttributes('tags'), 
        'infopage': node.getAttributes('infopage'), 
        'quality': node.getAttributes('quality'), 
        'id': node.getAttributes('id') 
      }) 

     except LoadPackageListFailed: 
      print("Loading Package List failed... try again soon or manually update this release!") 
      Write2ErrorLog(LoadPackageListFailed) 
#Indent Here Fails    
def Usage(): 
#prints usage and closes 
    print ("Invalid Argument Specified, please retry using the format stated below\n") 
    print ("*** Simtho Usage Parameters ***\n") 
    print ("-i Installs Local App, include full path") 
    print ("-u Uninstalls Installed App,include ID or Name") 
    print ("-l Lists all installed Apps and their ID") 
    print ("-a Lists All Apps in Repository") 
    print ("-s Downloads and Installs App from repository, enter the title or id number") 
    print ("-w Downloads and Installs Single App from a full link") 
    print ("-r Removes All Packages installed\n") 
    print ("*** End of Simtho Usage ***") 
    os._exit(1) 
    return; 
+2

缩进的位置究竟是什么?你能展示实际的产出和你的期望吗? – Rory

+1

尝试通过显示空白的东西运行它。在* nix机器上,使用'od -c yourfile.py'来查看字符。这可能是有用的,虽然可能有更好的方法来做到这一点。 – Ibrahim

+0

如果您还没有弄清楚,请复制上面的代码并粘贴到您自己的代码上面,它应该可以解决问题。如果没有,问题不在上面的代码中,而是在相邻的地方,正如〜unutbu建议的那样。 –

回答

3

不应该在第2行的尝试有一个除了,最后还是与之相关联?或者这是我以前从未见过的一些新的Python成语吗?

+0

是的,这样做,因为我把一个不需要的尝试声明或W/E类的东西尝试是 –

+1

嘿cuzzin,你没有没有代码是葡萄干'LoadPackageListFailed' –

4

您发布的代码工作得很好。因此,请检查您发布的部分上方的行。如果这没有帮助,请发布确切的错误消息和更多的代码。

编辑:同时检查你没有标签和空格的混合。

编辑2:(响应OP发布的更多代码)。 啊哈。每个try需要except。在LoadPackageList的定义中,有两个try's。内部有一个except块,但外部块只是一个裸露的try

7

尝试运行它通过python -t并查看是否有标签和空间的混合物在那里的某处。

附注:使用optparse来处理命令行参数。它会让你的生活更轻松,并产生一个很好的一致的界面。