0
Need:
I need to "observe" a directory, every new file inserted in this directory I need to make a copy of this file and forward to an FTP server.
What I got:
I can upload the file via FTP, and I can check the files in the directory.
What I need:
I need something similar to Dropbox, a service "watching" the folder and trigger an action whenever a new file is created, preferably in real time with only the delay of sending by FTP.
I do not know if it has relevance but I will need to let this service running on machines with windows 10, 8 and 7.
Is there any way to do that in Python?
My attempt at solution:
I thought to preview the directory and rescue a list of the existing files there, then start a loop ifinito checking if the amount of files currently would equal the first count, if the last count gives a value greater than the one of the previous count is replaced the old list by the current one and create a list only with the items that were not in the old one, with the list of items that were not in the old list would send the files by FTP.
I do not know if it was clear my need, I tried to put as much information as possible. I hope you can help me.
I created this code:
from ftplib import FTP
import os, subprocess
import time
f = open("lista.txt", "w")
f.writelines(os.listdir("C:\\Users\\Afonso\\Desktop\\nov2018\\dirteste\\"))
f.close()
def upload(filename, diretorio):
print("Conectando ao servico FTP")
ftp = FTP('host')
print("Logando com Usuário de Senha")
ftp.login('senha', 'senha')
print("iniciando debug")
ftp.set_debuglevel(3)
print("trocando diretório")
ftp.cwd('public_html')
print("Preparando diretorio local e nome de arquivo.")
file = "C:/Users/Afonso/Desktop/nov2018/dirteste/" + filename
print("print conferindo se existe diretorio")
try:
print("Criando diretorio")
ftp.mkd('/'+diretorio)
except:
pass
print("enviando arquivo %s" %file)
diretorio = "/"+diretorio+"/"
ftp.storbinary("STOR " + diretorio + filename, open(file, 'rb'), 1024)
while True:
nova_lista = os.listdir("C:\\Users\\Afonso\\Desktop\\nov2018\\dirteste\\")
lista = open("lista.txt").readlines()
print(type(lista))
for file in nova_lista:
if file not in lista:
time.sleep(1)
upload(file, "teste")
del lista
del nova_lista
The problem is my fear of the process consuming too much of the computer. There would be a way to improve or some technique that is not applied that improves the consumption of the program?