File operations, the use of recursion can not quit.

  When writing a manual FTP, you need to write a function to select a file, the file return path.

  This is an alternative one-time recursive function values.

Import OS 

DEF choose_file ():
     the while True: 
        current_path = The os.getcwd () 
        path_list = the os.listdir (current_path)
         for index, name in the enumerate (path_list,. 1 ):
             Print (index, name) 
        Choice = INPUT ( ' Enter your choice, press 0 to return to the previous >>>: ' ) .strip ()
         IF choice == ' 0 ' : 
            os.chdir (os.pardir) 
            choose_file () 
            # return 
        IF  notchoice.isdigit ():
             Print ( ' Enter number ' )
             Continue 
        Choice = int (Choice) -. 1
         IF Choice Not  in Range (0, len (path_list)):
             Print ( ' Please enter the correct number ' )
             Continue 
        path_name = path_list [Choice] 
        path = the os.path.join (current_path, path_name)
         IF os.path.isdir (path_name): 
            the os.chdir (path_name) 
            choose_file () 
        the else :
            print(path)
            return path,path_name

choose_file()

  The problem is that when I run into a file that is not a folder, return to the path value, but does not end select the directory, continue to let me choose.

  So I chose them again and let me choose. . . (What problems)

  Later up a bit and found just switch the directory will let me choose once.

  In this case must be because recursive problem, when I entered in a recursive on a recursive loop while not over, as long as the end conditions return, it will return a lot of the return value.

  solution:

  1, once ended recursion.

  2. Set a global variable, when conditions reach a value of False, otherwise the value is True. At the beginning of the end of the function to write the condition statement.

  3. As long as a get value returns that value, the function returns the value one, the outermost layer of the relay function.

  The first really have no idea what method, for the time being to give up thinking.

  After the second method become such practice. Not OK, since data inside the while loop not be sent out only through the return function while the end.

Import OS 


isgo = True 

DEF choose_file ():
     Global isgo
     IF isgo == False:
         return 
    the while True: 
        current_path = The os.getcwd () 
        path_list = the os.listdir (current_path)
         for index, name in the enumerate (path_list,. 1 ):
             Print (index, name) 
        choice = the iNPUT ( ' Please enter your selection, press 0 to return to the previous >>>: ' ) .strip ()
         IF choice == ' 0 ': 
            The os.chdir (os.pardir) 
            choose_file () 
            return 
        IF  Not choice.isdigit ():
             Print ( ' Enter number ' )
             Continue 
        Choice = int (Choice) -. 1
         IF Choice Not  in Range (0, len (path_list) ):
             Print ( ' Please enter the correct number ' )
             Continue 
        path_name = path_list [Choice] 
        path = the 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()

 

 

  The third method:

Import OS 


DEF choose_file ():
     the while True: 
        current_path = The os.getcwd () 
        path_list = the os.listdir (current_path)
         for index, name in the enumerate (path_list,. 1 ):
             Print (index, name) 
        Choice = INPUT ( ' Enter your choice, press 0 to return to the previous >>>: ' ) .strip ()
         IF choice == ' 0 ' : 
            os.chdir (os.pardir) 
            return choose_file ()
         IF  notchoice.isdigit ():
             Print ( ' Enter number ' )
             Continue 
        Choice = int (Choice) -. 1
         IF Choice Not  in Range (0, len (path_list)):
             Print ( ' Please enter the correct number ' )
             Continue 
        path_name = path_list [Choice] 
        path = the os.path.join (current_path, path_name)
         IF os.path.isdir (path): 
            the os.chdir (path) 
            return choose_file ()
         the else :
            # print(path)
            # isgo = False
            return path,path_name

print(choose_file())

  Note that when this method every time the function is called only once parentheses, otherwise you will repeatedly run the program.

  So to conclude:
  1. Do nothing to write an infinite loop, the while adding a second method to determine the conditions should be achieved.

  2. Recursion while inside do not add up. But also consider the while loop, but also consider the recursive loop problem, two termination condition can not exist together.

 

Guess you like

Origin www.cnblogs.com/LZXlzmmddtm/p/11360766.html