Created
August 4, 2021 20:42
-
-
Save user0able/5d84b5e48cacbe2d244b15c8e19a7148 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ftplib | |
import csv | |
import pandas as pd | |
from credentials import ( | |
HOST, | |
USER, | |
PASSWORD, | |
ROUTE | |
) | |
FILENAME = 'filenames_downloaded.csv' | |
def save_filenames_in_csv(*args, filename=FILENAME): | |
with open(filename, 'w') as csvfile: | |
write = csv.writer(csvfile) | |
write.writerows(args) | |
pd.read_csv(filename) | |
def unnamed_function(*files_to_compare, filename=FILENAME): | |
# open file and list, and return list of new files | |
try: | |
open(FILENAME, 'x') | |
except FileExistsError: | |
existing_filenames = [] | |
# Lee el csv existente: | |
with open(filename) as csvfile: | |
reader = csv.reader(csvfile) | |
for row in reader: # deja en la variable existing_filenames los nombres existentes | |
existing_filenames.append(row) | |
old_files = existing_filenames[0] | |
# print(type(existing_filenames[0])) # todo: bug?? | |
temp_files = old_files.copy() | |
for i in files_to_compare: # ve si está el new file en los temp_files | |
if i in temp_files: | |
temp_files.remove(i) # remueve cada new_file ya existente | |
return temp_files # retorna los archivos no existentes en el csv | |
def ftp_connect(HOST, USER, PASSWORD, ROUTE): | |
# conecta y descarga los archivos | |
ftp = ftplib.FTP( | |
host=HOST, | |
user=USER, | |
passwd=PASSWORD | |
) | |
ftp.cwd(ROUTE) | |
try: | |
t = unnamed_function(ftp.nlst()) | |
for i in t: | |
with open(i, 'wb') as file: | |
ftp.retrbinary('RETR %s' % i, file.write) | |
except IndexError: | |
save_filenames_in_csv(ftp.nlst()) | |
print("Vuelve a ejecutar: se llenó el archivo csv") | |
except TypeError: | |
print("Vuelve a ejecutar: se creó el archivo csv") | |
ftp.quit() | |
ftp_connect( | |
HOST, | |
USER, | |
PASSWORD, | |
ROUTE | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Entonces:
y de ahí en adelante, si se crea otro archivo lo descargará