简体   繁体   English

在python中从ftp文件夹和子文件夹读取所有文件

[英]Reading all files from ftp folders and subfolders in python

I want to read all the image files (*.jpg) from all the folders and subfolders of a ftp folder in memory, but not necessarily download them. 我想从内存中ftp文件夹的所有文件夹和子文件夹中读取所有图像文件(* .jpg),但不必下载它们。 The folder structure varies in depth and the files I need can be in the main folder or in any subfolder. 文件夹结构的深度不同,我需要的文件可以位于主文件夹或任何子文件夹中。

I tried using nlst in a loop, but I am just getting a listing of files and haven't been able to read the jpg files individually. 我尝试在循环中使用nlst,但我只是获得文件列表,而无法单独读取jpg文件。 Once I read one file, I plan to do some processing on the file using opencv and extract specific information in an array and move on to the next file. 读取一个文件后,我计划使用opencv对文件进行一些处理,并提取数组中的特定信息,然后移至下一个文件。

Here is my code: I haven't been able to navigate through subfolders yet. 这是我的代码:我尚未能够浏览子文件夹。

log = []
file_list = []
for name in ftp.nlst():
        print "listing: " + name
        ftp.cwd(name)
        ftp.retrlines('LIST',callback=log.append)
        #lines = lines.split("\n") # This should split the string into an array of lines
        #filename_index = len(lines[0]) - 1
        files = (line.rsplit(None, 1)[1] for line in log)
        for file in files:
            if file.split('.')[-1] == "jpg":            
            # whatever
                    file_list.append(file)
        ftp.cwd('../')

Any help is appreciated. 任何帮助表示赞赏。

Use the os import and pass the ftp folder or subfolder path in the listdir function. 使用os import并在listdir函数中传递ftp文件夹或子文件夹路径。

import os

for file in os.listdir('<INSERT FTP FULL PATH>'):
    if file.endswith(".jpg"):
        print(file)
        ...
        or do other python processing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python计算所有文件夹/子文件夹中的所有文件 - Count all files in all folders/subfolders with Python Python:如何保存文件夹和子文件夹中的文件? - Python: How to save files from folders and subfolders? Python-使用python ftplib模块下载所有文件夹,子文件夹和文件 - Python - download all folders, subfolders, and files with the python ftplib module Python:如何在ALL文件,文件夹和子文件夹的名称中用下划线替换空格? - Python: How to replace whitespaces by underscore in the name of ALL files, folders and subfolders? 从 ftp 服务器读取文件到 DataFrame python - Reading files from ftp server to DataFrame python 遍历文件夹,python中文件的几个子文件夹 - traversing folders, several subfolders for the files in python 使用python删除文件夹和子文件夹中的pdf文件? - Delete pdf files in folders and subfolders with python? 使用python复制文件夹和子文件夹,但只复制子文件夹中的第一个文件 - Copy folders and subfolders, but only first files in subfolders with python 从子文件夹和文件夹中读取文本文件,并在大熊猫中为每个文件文本创建一个数据框,以作为一种观察 - Reading text files from subfolders and folders and creating a dataframe in pandas for each file text as one observation 从 Python 中的 FTP 读取 CSV 文件不读取所有行 - Reading CSV file from FTP in Python not reading all rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM