How can I make os.walk
traverse the directory tree of an FTP database (located on a remote server)? The way the code is structured now is (comments provided):
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)
I am thinking that I need to define a new function (i.e., def ftp()
) to add this functionality to the code above. However, I am afraid that the os.walk
function will, by default, only walk the directory trees of the computer that the code is run from.
Is there a way that I can extend the functionality of os.walk
to be able to traverse a remote directory tree (via FTP)?
ftplib
. Is this possible to do? Disclaimer: I've already triedftptool
and could not get it to do what I want. As such, the code above is a Python rehash of the Linuxfind
command. I'm trying to extend it by incorporating an FTP switch toos.walk
. – Partisanftptool
in a way that works for remote FTP databases, I will accept this as an answer as well. – Partisanfind
command in the Terminal, it by default searches the directory tree structure of my system (usually starting from the home directory). However, I am looking for a way to tellfind
to search the directory tree structure of a remote directory tree (such as any FTP database available on the web). Usually, you would need to open up a web browser and navigate to this FTP site. However, I would like to connect to it externally via a Python script and then use thefind
command to search it. – Partisanlocate fname
is much much faster and it runs on most linux machines – Izylocate fname
in the code of your answer anywhere? – Partisan