[英]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.