PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 1, 2022

[FIXED] How to download specified extension type of files from FTP server using python

 August 01, 2022     ftp, python     No comments   

Issue

I am trying to download each .txt file from my FTP server using python but as of now I only have one file being downloaded that is specified by it's name. Does anyone know the way I can change it to download all of the .txt files from my server?

    # connect to the FTP server
    ftp = FTP(FTP_HOST, FTP_USER, FTP_PASS)

    ftp.encoding = "utf-8"

    ftp.cwd('/files')
    ftp.retrlines('LIST')

    with open('test.txt', 'wb') as fp:
        ftp.retrbinary('RETR test.txt', fp.write)

    ftp.quit()

Solution

Something like this should work fine:

import fnmatch

with FTP(FTP_HOST,FTP_USER,FTP_PASS) as ftp:
        ftp.encoding = "utf-8"
        ftp.cwd('/files')
        for filename in ftp.nlst():
            if fnmatch.fnmatch(filename, '*.txt'):
                 with open(filename, 'wb') as fp:
                      ftp.retrbinary(f'RETR {filename}', fp.write)


Answered By - Christian Sloper
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing