Làm cách nào để tạo os.walk
duyệt cây thư mục của cơ sở dữ liệu FTP (nằm trên máy chủ từ xa)? Cách mã được cấu trúc tại là (bình luận cung cấp):Mở rộng chức năng os.walk của Python trên máy chủ 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)
Tôi nghĩ rằng tôi cần phải xác định một chức năng mới (ví dụ: def ftp()
) để thêm chức năng này vào các mã trên. Tuy nhiên, tôi sợ rằng chức năng os.walk
sẽ, theo mặc định, chỉ đi bộ các cây thư mục của máy tính mà mã được chạy.
Có cách nào để tôi có thể mở rộng chức năng của os.walk
để có thể duyệt qua cây thư mục từ xa (qua FTP) không?
https: // pypi .python.org/pypi/ftptool/0.5.1 –
Tôi đang cố gắng tránh bất kỳ giao diện nào ngoài 'ftplib'. Đây có phải là có thể làm gì? Disclaimer: Tôi đã cố gắng 'ftptool' và không thể làm cho nó để làm những gì tôi muốn. Như vậy, đoạn mã trên là bản phục hồi Python của lệnh 'find' của Linux. Tôi đang cố gắng để mở rộng nó bằng cách kết hợp một chuyển đổi FTP để 'os.walk'. – warship
Nếu ai đó có thể chỉ cho tôi cách thực hiện lại điều này trong 'ftptool' theo cách hoạt động cho cơ sở dữ liệu FTP từ xa, tôi cũng sẽ chấp nhận điều này như một câu trả lời. – warship