我有一个函数可以将文件从一台服务器移动到另一台服务器。该函数在执行时返回文件名,如果没有传输文件则返回 False。
我想循环调用这个函数,直到它返回 False。我还需要在每次调用函数时访问返回值(文件名)。我可以做其中之一,但我在做这两件事时遇到困难。这是我试图用伪代码完成的任务:
移动文件的函数(这不会改变):
def move_first_matching_file():
try:
# find the first file that matches a wildcard file name
# move the file from remote server to local server
# delete file from remote server
return name_of_file
except:
return False
在其他模块中调用上述函数(这需要工作):
while move_first_matching_file() as name_of_file is not False:
# process name_of_file
我需要完成上面的 while 循环,但也需要访问返回的文件名。我该怎么做呢?我上面的代码显然不起作用,但它概述了我想要实现的目标。
最佳答案
你不能在Python中做这样的事情,因为赋值始终是一个语句。通常的模式是:
while True:
name_of_file = move_first_matching_file()
if not name_of_file:
break
...
关于python - 执行一个函数以在循环中返回一个值,直到该函数返回 False - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37924937/