Skip to content Skip to sidebar Skip to footer

Execute A Function To Return A Value On A Loop Until That Function Returns False - Python

I have a function that moves a file from one server to another. The function returns the file name when executed or it returns False if no file was transferred. I would like to ca

Solution 1:

You can't do things like that in Python, as assignment is always a statement. The usual pattern is:

while True:
    name_of_file = move_first_matching_file()
    if not name_of_file:
        break
    ...

Solution 2:

As said in comment, you can also do

filename = move_first_matching_file()
while filename:
    # process file# call the function again and reassing the filename
    filename = move_first_matching_file()

Because your function send a string if successful (which always stands for true), and false if unsuccessful. So the loop while break if the function fails, but goes on if a file name is found

Post a Comment for "Execute A Function To Return A Value On A Loop Until That Function Returns False - Python"