简体   繁体   English

使用 python 从 ftp 自动下载文件并防止下载文件超过一次

[英]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.我们有一个 ftp 文件夹,其中包含 700 多个文件(自动从我们的系统生成),文件名如下: ff9a6c2b-4222-4464-a314-dce56efe76cb.txt fffa92f0-9e76-4cce-8933-d256bac90ffa.txt ffbaef37-9d73-41d5-8f23-7ca7c82b3969.txt .. fffa92f0-9e76-4cce-8933-d256bac90ffa.txt我正在尝试编写一个脚本来自动下载文件并防止多次下载文件,我的想法是将所有下载的文件名存储在 xml 文件中(它运行良好)并检查是否该名称在下载前存在于 xml 文件中。

The problem is even if there is 8 new files it download only one.问题是即使有 8 个新文件,它也只下载一个。

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我通过将从 xml 提取的文件名存储到一个列表中解决了这个问题,如果其他人和我有同样的问题,这里是下面的代码


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!") 


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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