简体   繁体   English

通过FTP根据文件名上传多个文件

[英]Uploading multiple files via FTP based on filename

Using Python 3.4 使用Python 3.4

I am generating files that are will look like 'Report_XXXXXX.xlsx' with X being unique customer numbers. 我生成的文件看起来像“ Report_XXXXXX.xlsx”,其中X是唯一的客户编号。 I have a server with folders that are named 'XXXXXX.CustomerName'. 我有一台服务器,其文件夹名为“ XXXXXX.CustomerName”。 I am trying to loop through each file/report and upload it to the matching folder based on the customer no. 我试图遍历每个文件/报告,并根据客户编号将其上传到匹配的文件夹。 I have something that is working in my small test environment but I don't think it is working how I want. 我有一些可以在小型测试环境中工作的东西,但我认为它并没有达到我想要的效果。 It uploads the files, but I am trying to catch anytime it doesn't find a match. 它会上传文件,但是我试图在找不到匹配项的任何时间进行捕获。 Currently it fails my IF statment for every file. 目前,每个文件都无法通过我的IF声明。 I think I am looping too many times or over the wrong items. 我想我循环了太多次或遍历了错误的项目。

import os
import ftplib

creds = [line.rstrip('\n') for line in open('C:\\folder\\credentials.txt')]
ftp = ftplib.FTP_TLS("server.com")
ftp.login(creds[0], creds[1])
ftp.prot_p()
src_dir = 'C:\\Reports\\'
src_files = os.listdir('C:\\Reports\\')

for folder_name in ftp.nlst():
    for file_name in src_files:
       if folder_name[0:6] == file_name[7:-5]:
            ftp.cwd('/'+folder_name)
            open_file = open(src_dir+file_name, 'rb')
            ftp.storbinary('STOR '+file_name, open_file)
            open_file.close()
       else:
        print('Folder ' + folder_name + ' Not Found')
ftp.quit()

So for example the source directory has 3 files: 'Report_100002.xlsx, Report_100003.xlsx, Report_100007.xlsx' And the server has matching folders and a few extra folders. 因此,例如,源目录具有3个文件:'Report_100002.xlsx,Report_100003.xlsx,Report_100007.xlsx'并且服务器具有匹配的文件夹和一些其他文件夹。 The files upload, and the output looks like so: 文件上传,输出看起来像这样:

Folder 100000.CustomerName Not Found
Folder 100000.CustomerName Not Found
Folder 100000.CustomerName Not Found
Folder 100002.CustomerName Not Found
Folder 100002.CustomerName Not Found
Folder 100003.CustomerName Not Found
Folder 100003.CustomerName Not Found
Folder 100007.CustomerName Not Found
Folder 100007.CustomerName Not Found

I am trying to get to a state where I can properly log each item and whether it was a success, what folder it landed in, etc... 我试图进入一种状态,在此状态下,我可以正确记录每个项目以及它是否成功,放置在哪个文件夹中,等等。

In your inner for loop you compare all 3 file names in src_dir with folder_name , but maximally only one satisfies the condition in your if statement. 在您的内部for循环中,您将src_dir所有3个文件名与folder_name进行比较,但是最多只有一个满足if语句中的条件。 So the other 2 or 3 files that don't match cause the output you are seeing, for every folder on the ftp server. 因此,对于ftp服务器上的每个文件夹,其他2个或3个不匹配的文件都会导致您看到输出。 You could use a flag to keep track of whether a match was found and print your output based on that flag. 您可以使用标志来跟踪是否找到匹配项,并根据该标志打印输出。

Another thing is that you should start iterating over src_files and then find matching folder names by iterating over ftp.nlist() (you are interested in source files that don't have a matching folder, not the other way around). 另一件事是,您应该开始遍历src_files ,然后通过遍历ftp.nlist()查找匹配的文件夹名称(您对没有匹配文件夹的源文件很感兴趣, ftp.nlist() )。 So something like this (assuming a source file is allowed to end up in multiple folders): 因此,如下所示(假设源文件可以在多个文件夹中结束):

....
folder_names = ftp.nlst()
for file_name in src_files:
    folder_found = False
    for folder_name in folder_names:
        if folder_name[0:6] == file_name[7:-5]:
            folder_found = True
            ftp.cwd('/'+folder_name)
            open_file = open(src_dir+file_name, 'rb')
            ftp.storbinary('STOR '+file_name, open_file)
            open_file.close()
    if not folder_found:   
        print('No destination folder found for ' + file_name)
ftp.quit()

(the folder_names = ftp.nlst() is there so you don't repeatedly list the directories on the server) folder_names = ftp.nlst()在那里,因此您不必重复列出服务器上的目录)

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

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