1

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. 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. 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.

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.

Eddy
  • 11
  • 3

1 Answers1

-2

Use the os import and pass the ftp folder or subfolder path in the listdir function.

import os

for file in os.listdir('<INSERT FTP FULL PATH>'):
    if file.endswith(".jpg"):
        print(file)
        ...
        or do other python processing
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • I am getting en error when using FTP full path with os.listdir. – Eddy Jan 19 '15 at 15:10
  • Sorry for the confusion. You must actually insert the FTP path between the single quotes. Let me edit my answer. – Parfait Jan 19 '15 at 17:41
  • I must be doing something wrong here. I tried that, but still get a WindowsError as WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'ftp://ftp.xyz.com/abc%20Creative/Site%20Specific%20Creative/*.*' – Eddy Jan 19 '15 at 22:15
  • Then, your FTP path is not an accessible path on your operating system (os) like a virtual directory. You will have to use the ftplib module. See this [SO answer](http://stackoverflow.com/questions/5230966/python-ftp-download-all-files-in-directory). – Parfait Jan 20 '15 at 02:49