Come si può fare os.walk
attraversare l'albero delle directory di un database FTP (situato su un server remoto)? Il modo in cui il codice è strutturato ora è (commenti forniti):Estensione della funzione os.walk di Python sul server FTP
import fnmatch, os, ftplib
def find(pattern, startdir=os.curdir): #find function taking variables for both desired file and the starting directory
for (thisDir, subsHere, filesHere) in os.walk(startdir): #each of the variables change as the directory tree is walked
for name in subsHere + filesHere: #going through all of the files and subdirectories
if fnmatch.fnmatch(name, pattern): #if the name of one of the files or subs is the same as the inputted name
fullpath = os.path.join(thisDir, name) #fullpath equals the concatenation of the directory and the name
yield fullpath #return fullpath but anew each time
def findlist(pattern, startdir = os.curdir, dosort=False):
matches = list(find(pattern, startdir)) #find with arguments pattern and startdir put into a list data structure
if dosort: matches.sort() #isn't dosort automatically False? Is this statement any different from the same thing but with a line in between
return matches
#def ftp(
#specifying where to search.
if __name__ == '__main__':
import sys
namepattern, startdir = sys.argv[1], sys.argv[2]
for name in find(namepattern, startdir): print (name)
Sto pensando che ho bisogno di definire una nuova funzione (vale a dire, def ftp()
) per aggiungere questa funzionalità al codice di cui sopra. Tuttavia, temo che la funzione os.walk
esegua, per impostazione predefinita, solo le strutture di directory del computer da cui viene eseguito il codice.
Esiste un modo per estendere la funzionalità di os.walk
per poter attraversare un albero di directory remoto (tramite FTP)?
https: // PyPI .python.org/pypi/ftptool/0.5.1 –
Sto cercando di evitare qualsiasi interfaccia oltre a 'ftplib'. È possibile farlo? Disclaimer: ho già provato 'ftptool' e non sono riuscito a farlo fare quello che voglio. Come tale, il codice sopra è un rehash Python del comando 'find' di Linux. Sto cercando di estenderlo incorporando un interruttore FTP in 'os.walk'. – warship
Se qualcuno può mostrarmi come reimplementare questo in 'ftptool' in un modo che funzioni per database FTP remoti, accetterò anch'io una risposta. – warship