python - 执行一个函数以在循环中返回一个值,直到该函数返回 False - Python

标签 python

我有一个函数可以将文件从一台服务器移动到另一台服务器。该函数在执行时返回文件名,如果没有传输文件则返回 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/

相关文章:

python - sklearn如何根据特征选择来选择分类特征

python - 一个 Django 项目 - 根据域呈现不同的内容

python - 多对多领域 - Django 模型 - 理解

python - 字典 python 中的匹配值

python - 使用Pyglet批量绘制GL_LINE_STRIP

python - 初始化对象变量——一种 Java 方法,一种 Python 方法?

python - pandas dataframe to_dict 两列作为索引,第三列作为值

python - Subversion 提交后钩子(Hook)

python - 挑战 : TSP problem and finding the right minimized order of points

python - pandas.replace 与 str.replace 正则表达式冲突。代码顺序