简体   繁体   中英

auto download files from ftp using python and prevent downloading files more than one time

We have a ftp folder that contain more than 700 file (generetad automaticly from our system) with file name like: ff9a6c2b-4222-4464-a314-dce56efe76cb.txt .. ffbaef37-9d73-41d5-8f23-7ca7c82b3969.txt .. fffa92f0-9e76-4cce-8933-d256bac90ffa.txt I am trying to write a script to auto download files and prevent downloading files more than one time, my idea is to store all downloaded file names in xml file (it worked well) and check if the name exist in xml file before downloading.

The problem is even if there is 8 new files it download only one.

My code

import ftputil 
from subprocess import Popen 
import ftplib
import os
import os.path
import xml.etree.cElementTree as ET



server = 'ip_adress'
user = 'user'
password = 'Passwd'


ftp = ftplib.FTP(server)
ftp.login(user, password)

files = []
root = ET.Element("files")

file_exists = os.path.isfile("filename.xml") 

if file_exists:
   f = ET.parse("filename-Copie.xml")
   root = f.getroot()
   files = ftp.nlst()
   for file in files:
       for item in root:
           if file != item.text :
               #print ("the file name: ",item.text, " already exist")  
                ftp_host = ftputil.FTPHost(server, user, password)
                ftp_host.download(file, file)
                ET.SubElement(root, "file", name="filename").text = file

   tree = ET.ElementTree(root)
   tree.write("filename.xml")
   p = Popen("move.bat", cwd=r"C:\\Users\\Sas\\Desktop\\ftpdown\\") 
   stdout, stderr = p.communicate()
   print("Finished!") 

else:
   f = open("filename.xml", "w")
   with ftputil.FTPHost(server, user, password) as ftp_host: 

        files = ftp_host.listdir(ftp_host.curdir)
        root = ET.Element("files")
        for file in files:
            if ftp_host.path.isfile(file):
               ftp_host.download(file, file)
               ET.SubElement(root, "file", name="filename").text = file
   tree = ET.ElementTree(root)
   tree.write("filename.xml")
   p = Popen("move.bat", cwd=r"C:\\Users\\Sas\\Desktop\\ftpdown\\") 

   stdout, stderr = p.communicate() 
print("Finished!") 


I solved the Problem by storing file name extracted from xml into a list here is the code bellow if someone else have the same problem as me


import ftputil 
from subprocess import Popen 
import ftplib
import os
import os.path
import xml.etree.cElementTree as ET



server = 'ip-adress'
user = 'user'
password = 'Passwd'


ftp = ftplib.FTP(server)
ftp.login(user, password)

files = []
root = ET.Element("files")

file_exists = os.path.isfile("filename.xml") 

if file_exists:
   f = ET.parse("filename-Copie.xml")
   root = f.getroot()
   files = ftp.nlst()
   oldfile = []
   for child in root:
       oldfile.append(child.text)

   for file in files:
           if file not in oldfile: 
                ftp_host = ftputil.FTPHost(server, user, password)
                ftp_host.download(file, file)
                ET.SubElement(root, "file", name="filename").text = file

   tree = ET.ElementTree(root)
   tree.write("filename.xml")
   p = Popen("move.bat", cwd=r"C:\\Users\\Sas\\Desktop\\ftpdown\\") 
   stdout, stderr = p.communicate()
   print("Finished!") 

else:
   f = open("filename.xml", "w")
   with ftputil.FTPHost(server, user, password) as ftp_host: 

        files = ftp_host.listdir(ftp_host.curdir)
        root = ET.Element("files")
        for file in files:
            if ftp_host.path.isfile(file):
               ftp_host.download(file, file)
               ET.SubElement(root, "file", name="filename").text = file
   tree = ET.ElementTree(root)
   tree.write("filename.xml")
   p = Popen("move.bat", cwd=r"C:\\Users\\Sas\\Desktop\\ftpdown\\") 

   stdout, stderr = p.communicate() 
print("Finished!") 


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM