Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:54:02+00:00 2026-06-13T09:54:02+00:00

I have a script that pulls files from a FTP directory on a set

  • 0

I have a script that pulls files from a FTP directory on a set interval. However, since the files are only being copied, not moved, it ends up pulling the same files over and over again. What’s the best way to ensure that I am only pulling new files? I’m thinking of cross referencing the files on the FTP site with the files in the local directory, but not quite sure how to do that. Also, how would I go about not only checking file names, but modified dates as well? For example: random_file.txt was originally placed on the FTP site on 10/25/2012 at 2:15 pm and was downloaded 5 minutes later. Then, on 10/26/2012 at 11:40 am, random_file.txt was replaced on the FTP site with an updated version. Can I download from the FTP site and/or overwrite files on the local directory only the newer files? Thanks!

Here is my existing code:

import ftplib, os

def fetch():
    server = 'ftp.example.com'
    username = 'foo'
    password = 'bar'
    directory = '/random_directory/'
    filematch = '*.txt'
    ftp = ftplib.FTP(server)
    ftp.login(username, password)
    ftp.cwd(directory)
    for filename in ftp.nlst(filematch):
        fhandle = open(os.path.join('C:my_directory', filename), 'wb')
        print 'Getting ' + filename
        ftp.retrbinary('RETR ' + filename, fhandle.write)
        fhandle.close()

UPDATE: So I used Siddharth Toshniwal’s links to figure it out, partially at least. For those that may stumble across this and need it, here is my new code so far. Note this only checks for the existence of the file, not it’s modified date:

for filename in ftp.nlst(filematch):
        if os.path.exists('C:\my_directory\\' + filename) == False:
            fhandle = open(os.path.join('C:\my_directory', filename), 'wb')
            print 'Getting ' + filename
            ftp.retrbinary('RETR ' + filename, fhandle.write)
            fhandle.close()
        elif os.path.exists(('C:\my_directory\\' + filename)) == True:
            print 'File ', filename, ' Already Exists, Skipping Download'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:54:04+00:00Added an answer on June 13, 2026 at 9:54 am

    I second the opinion of using something like rsync rather than hack something in python.

    But for whatever reason if that is not feasible, the following links should help you:
    http://code.activestate.com/recipes/327141-simple-ftp-directory-synch/
    http://alexharvey.eu/code/python/get-a-files-last-modified-datetime-using-python/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that pulls in data from many sources (databases, files,
I have an ant build script that needs to pull files down from a
I have been asked to write a script that pulls the latest code from
I have following script that executes all the .reg files in the current directory.
I have a server with a PHP script that pulls data from a source
I think we have a problem in our FTP scripts that pull files from
I have this action script that pulls images from a xml file > myPhoto
I have a script that pulls an image from SQL Server and displays it
I have been working on a script that pulls information from a certain website.
SO I have a script that pulls information from an event webpage. URL is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.