尝试只从FTP文件夹下载特定文件

2024-05-15 09:10:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网上找到了一些从FTP文件夹下载所有文件的代码。我想稍微修改一下代码,只下载符合特定命名约定的文件。例如,我只想下载名称中有“ARM”的文件。我认为下面的代码很接近,但我收到一条错误消息,内容如下:

TypeError: 'in <string>' requires string as left operand, not tuple

错误发生在以下行:if file in 'ARM':

下面是我的代码示例(这不是全部代码;只是尽量保持简单)。你知道吗

for file in filelist:
    if file in 'ARM':
        if file[1]['type'] == 'file':
            fullpath = os.path.join(destination[:-1] + path, file[0])
            if (not overwrite and os.path.isfile(fullpath)):
                continue
            else:
                with open(fullpath, 'wb') as f:
                    ftp.retrbinary('RETR ' + file[0], f.write)
                print(file[0] + '  downloaded')
        elif file[1]['type'] == 'dir':
            fetchFiles(ftp, path + file[0] + '/', destination, overwrite)
    else:
        print('Unknown type: ' + file[1]['type'])

我想我需要循环一个元组的元素。是这样吗?我可以简单地检查文件名以找到匹配的字符串吗?你知道吗


Tags: 文件path代码instringifosas