SturgeonGeneral
SturgeonGeneral

Reputation: 23

Uploading multiple files via FTP based on filename

Using Python 3.4

I am generating files that are will look like 'Report_XXXXXX.xlsx' with X being unique customer numbers. I have a server with folders that are named '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. 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. 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...

Upvotes: 1

Views: 1538

Answers (1)

jaapvee
jaapvee

Reputation: 141

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. So the other 2 or 3 files that don't match cause the output you are seeing, for every folder on the ftp server. 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). 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)

Upvotes: 1

Related Questions