开发者

Why Unexpected Indent?

开发者 https://www.devze.com 2022-12-15 16:46 出处:网络
Why does this happen? def LoadPackageList(): try: #Attempts to load package list... Adds each neccessary attribute into array

Why does this happen?

def LoadPackageList():
    try:
        #Attempts to load package list... Adds each neccessary attribute into array
        print("Loading Package List... please wait")
        packages = []
        packagelisturl = os.开发者_Go百科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;


Try running it through python -t and see if there is a mixture of tabs and spaces somewhere in there.

As a side note: use optparse to process command line parameters. It will make your life much easier and produce a nice consistent interface.


The code you posted works just fine. Therefore, check the lines immediately above the part you posted. If that does not help, please post the exact error message and a little more of the code.

Edit: Also check that you do not have a mixture of tabs and spaces.

Edit 2: (In response to more code posted by the OP). Aha. Every try requires an except. In the definition of LoadPackageList, there are two try's. The inner one has an except block, but the outer one is just a bare try.


Shouldn't the try on line 2 have an except, finally or else associated with it? Or is this some new Python idiom I haven't seen before?

0

精彩评论

暂无评论...
验证码 换一张
取 消