Skip to content Skip to sidebar Skip to footer

Python Ftp Download File With Certain Name

I have this FTP with folder and it contains these files: pw201602042000.nc, pw201602042010.nc, pw201602042020.nc, pw201602042030.nc, pw201602042040.nc, pw201602042050.nc, p

Solution 1:

when you obtained the list of files as list_of_files, just use fnmatch to match the file names according to wildcard:

list_of_files = server.retrlines("LIST")
dest_dir = "."for name in list_of_files:
    if fnmatch.fnmatch(name,"*00.nc"):
        with open(os.path.join(dest_dir,name), "wb") as f:
            server.retrbinary("RETR {}".format(name), f.write)  

(note that you're writing the files on the same "pw" output file, I changed that, reusing the original name and provided a destination directory variable, and protecting the open in a with block to ensure file is closed when exiting the block)

Post a Comment for "Python Ftp Download File With Certain Name"