python try/except/else with recursion [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-07 04:31:04

问题


Python version: 2.7. OS: Windows 10 64-bit.

Note: I have found a way around the issue described below that doesn't use try/except/else statements. I am asking the question below only because I am curious as to why the code behaves the way it does, and if there is a way to do what I am trying to do using try/except/else.

I have a file called blah.py, with the following code:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num)) #error if argument of os.mkdir already exists
    except:
        makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

Now I go to Powershell, and type:

import blah
myStr = blah.makeFolder('myFolder')
print myStr
print type(myStr)

It does what I would expect - a folder called myFolder_0 is created, and it prints myFolder_0 and <type 'str'>. Now, still in Powershell, I type:

myStr1 = blah.makeFolder('myFolder')
print myStr1
print type(myStr1)

This time it makes a folder called myFolder_1, as I expect, but instead of printing myFolder_1 and <type 'str'>, it prints None and <type 'NoneType'>. It will continue doing this every subsequent time I use blah.makeFolder('myFolder').

The behavior is also weirdly different if I put the commands I typed in Powershell inside of the script. I made a file called blah2.py, which is the same as blah.py, but with a script at the end:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num)) #error if argument of os.mkdir already exists
    except:
        makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

myStr = makeFolder('myFolder')
print myStr
print type(myStr)

myStr1 = makeFolder('myFolder')
print myStr1
print type(myStr1)

Then in Powershell:

python blah2.py

This time it makes myFolder_0 and prints myFolder_0 and <type 'str'>, (so the myStr block works as in blah.py), and then goes into an infinite recursion (so the myStr1 block doesn't work). So for reasons I don't understand, the behavior is different than it is during the interactive session. If I type python blah2.py again, it makes myFolder_1 and prints None and <type 'NoneType'> (myStr block), then goes into infinite recursion again (myStr1 block).

Why does the script behave differently than the interactive session, why does infinite recursion happen in the script, and is there a version of my code that still uses try/except/else, but works?


回答1:


Your code works fine for me if I add a return to the recursive call:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num))
    except OSError:
        return makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)

print(makeFolder('myFolder')) # myFolder_0
print(makeFolder('myFolder')) # myFolder_1

As to why you're seeing what you're seeing... there's definitely something else going on here. The code you shared for blah2.py couldn't possibly work, since blah isn't defined anywhere. My guess is that you're running different code without realizing it. (Maybe a different copy of the file in a different directory?)




回答2:


My first thought is that the inconsistent behavior you're seeing might be because some directories already exist if you're running them one after the other. To prevent this, try adding this code to the beginning to clean up any directories created by a previous run:

import os

for dirname in [f for f in os.listdir('.') if 
                (os.path.isdir(f) and f.startswith('myFolder'))]:
     os.rmdir(dirname)

But here's how to fix the actual issue:

import os

def makeFolder(dirName, num = 0):
    try:
        os.mkdir(dirName + '_' + str(num)) #error if argument of os.mkdir already exists
    except:
        return makeFolder(dirName, num = num + 1)
    else:
        return dirName + '_' + str(num)


来源:https://stackoverflow.com/questions/39059566/python-try-except-else-with-recursion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!