python FTP download file with certain name

后端 未结 1 1331
[愿得一人]
[愿得一人] 2021-01-21 07:09

I have this FTP with folder and it contains these files:

pw201602042000.nc,
 pw201602042010.nc,
 pw201602042020.nc, 
 pw201602042030.nc, 
 pw201602042040.nc,
 pw         


        
相关标签:
1条回答
  • 2021-01-21 08:01

    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)

    0 讨论(0)
提交回复
热议问题