Bottom Page

Thread Rating:
  • 2 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 ftplib os.listdir function
#1
Hello everyone,

I'm looking for a method like os.listdir(), but in the ftplib, within a ftp server. I want to list every dir on the ftp server and it's contents, if there's a dir inside dir list its contents as well. Here's where I have so far

import ftplib
ftpinstance = ftplib.FTP('ftp server','username','password') 
dirk = ''
listofitems = ''
def list_item(dirk,listofitems):
    if listofitems == '': 
        listofitems = []
        try:
            ftpinstance.retrlines('NLST',listofitems.append) #GETS files' names (dirs or files) of the current dir 
            mwd = ftpinstance.pwd()
            for item in listofitems:
                ftpinstance.cwd(mwd+'/'+item+'/')
                listofitems2 = []
                ftpinstance.retrlines('NLST',listofitems2.append) #IF not a dir here, error occurs prints it's a file.
                print ('Folder found:',item)
                cwd = ftpinstance.pwd()
                list_item(cwd,listofitems2)
        except Exception as e:
            print (e)
            print ('File Found:',item)
    else:
        mwd = ftpinstance.pwd()
        for item in listofitems:
            try:
                ftpinstance.cwd(mwd+'/'+item+'/')
                listofitems2 = []
                ftpinstance.retrlines('NLST',listofitems2.append)
                print ('Folder found:',item)
                cwd = ftpinstance.pwd()
                list_item(cwd,listofitems2)            
            except Exception as e:
                print (e)
                print ('File Found:',item)

list_item(dirk,listofitems)
I can't print out the content of several dirs (for 4 dirs inside) only of the first two(with this code) If you don't understand something in the code do not hesitate to ask me. The while point is really to make a similar os.list() command to FTP, I did not find one in the documentation of the library, plus I had a look at the os lib code, but I did not find the os.list dir command code.

I'm using python3.5 in Linux. Thank you in advance
Quote
#2
Your script runs well with some changes:

#!/usr/bin/python3
import ftplib

def list_item(dirk,listofitems):
    if listofitems == []:
        ftpinstance.retrlines('NLST',listofitems.append) # dirs or files of the current dir

    mwd = ftpinstance.pwd() # absolute path
    for item in listofitems:
        try:
            ftpinstance.cwd(mwd+'/'+item+'/')
            listofitems2 = []
            ftpinstance.retrlines('NLST',listofitems2.append) #if not a dir, error occurs
            print (dirk,'d',item)
            list_item(dirk+'  ',listofitems2)
        except Exception as e:
            print (dirk,'f',item)

#main
ftpinstance = ftplib.FTP('ftp.adobe.com','anonymous','anonymous')
ftpinstance.cwd("pub/adobe/acrobat/win/Acrobat2017")

print("start")
list_item('', [])
print("end")
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ftplib and pyftpdlib : REST (restart) command not working as expected when uploading darellon 3 1,949 Mar-05-2017, 01:11 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)