文件操作中,使用递归退出不了。

  在手动编写一个FTP时,需要编写一个函数用来选择文件,返回文件路径。

  这里选择了递归函数进行一次性的取值。

import os

def choose_file():
    while True:
        current_path = os.getcwd()
        path_list = os.listdir(current_path)
        for index,name in enumerate(path_list,1):
            print(index,name)
        choice = input('请输入您的选择,按0返回上一级>>>:').strip()
        if choice == '0':
            os.chdir(os.pardir)
            choose_file()
            # return
        if not choice.isdigit():
            print('请输入数字')
            continue
        choice = int(choice) - 1
        if choice not in range(0,len(path_list)):
            print('请输入正确的编号')
            continue
        path_name = path_list[choice]
        path = os.path.join(current_path,path_name)
        if os.path.isdir(path_name):
            os.chdir(path_name)
            choose_file()
        else:
            print(path)
            return path,path_name

choose_file()

  问题在于,当我执行到了非文件夹的文件后,返回到了路径值,但是并没有结束选择目录,继续让我选择。

  于是我又选了一遍,发现又让我选择。。。(啥毛病)

  后来总结了一下,发现只要切换了目录就会让我多选择一次。

  这样的话肯定是因为递归的问题了,在我进入下一次的递归时,上一个递归的while循环还没有结束,结束条件只要return,所以会返回很多的返回值。

  解决方案:

  1,一次性结束递归。

  2.设置一个全局变量,当条件达成时值为False,否则值为True。在函数开头写上结束语句的条件。

  3.只要一拿到值,就返回该值,上一层函数在返回该值,层层接力到最外层的函数。

  第一个实在想不出来有什么方法,暂且放弃思考。

  第二个方法实践后变成了这样。不太行,因为,while循环内部的数据传不出去,只能通过return结束while中的函数。

import os


isgo = True

def choose_file():
    global isgo
    if isgo ==False:
        return
    while True:
        current_path = os.getcwd()
        path_list = os.listdir(current_path)
        for index,name in enumerate(path_list,1):
            print(index,name)
        choice = input('请输入您的选择,按0返回上一级>>>:').strip()
        if choice == '0':
            os.chdir(os.pardir)
            choose_file()
            return
        if not choice.isdigit():
            print('请输入数字')
            continue
        choice = int(choice) - 1
        if choice not in range(0,len(path_list)):
            print('请输入正确的编号')
            continue
        path_name = path_list[choice]
        path = os.path.join(current_path,path_name)
        if os.path.isdir(path):
            os.chdir(path)
            choose_file()
            return
        else:
            # print(path)
            isgo = False
            return path,path_name


print(choose_file())
# choose_file()

  第三个方法:

import os


def choose_file():
    while True:
        current_path = os.getcwd()
        path_list = os.listdir(current_path)
        for index,name in enumerate(path_list,1):
            print(index,name)
        choice = input('请输入您的选择,按0返回上一级>>>:').strip()
        if choice == '0':
            os.chdir(os.pardir)
            return choose_file()
        if not choice.isdigit():
            print('请输入数字')
            continue
        choice = int(choice) - 1
        if choice not in range(0,len(path_list)):
            print('请输入正确的编号')
            continue
        path_name = path_list[choice]
        path = os.path.join(current_path,path_name)
        if os.path.isdir(path):
            os.chdir(path)
            return choose_file()
        else:
            # print(path)
            # isgo = False
            return path,path_name

print(choose_file())

  注意,这种方法每一次调用函数时,只能加括号一次,否则就会反复运行程序。

  所以得出结论:
  1.没事别写死循环,把while加一个判断条件应该可以实现第二种方法。

  2.递归里面就不要 加while了。又要考虑while循环体,又要考虑递归循环题,两个终止条件还不能一起存在。

原文地址:https://www.cnblogs.com/LZXlzmmddtm/p/11360766.html