0

I want to loop through this script every 10 minutes. I already tried some methods but it didn't work.

import ftplib
import datetime, time

#loop from this
ts = datetime.datetime.now().strftime('%Y-%m-%d--%H-%M-%S')
session = ftplib.FTP('localhost','user','password')
file = open('key_log.txt','rb')
session.storbinary('STOR '+str(ts)+'--key_log.txt', file)
file.close()
session.quit()
Emma
  • 27,428
  • 11
  • 44
  • 69
  • you will need a scheduler like aps, please go through this link, it will do the job for you https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python – young_minds1 Jul 20 '19 at 16:31

2 Answers2

1

The simplest way is just to put it in a loop:

import ftplib
import datetime, time

#loop from this
while True:
    ts = datetime.datetime.now().strftime('%Y-%m-%d--%H-%M-%S')
    session = ftplib.FTP('localhost','user','password')
    file = open('key_log.txt','rb')
    session.storbinary('STOR '+str(ts)+'--key_log.txt', file)
    file.close()
    session.quit()

    time.sleep(10 * 60)

If you want to actually use this, you probably want to look into cron.

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
0

If you want to keep it simple, here's a way using datetime and sleep

import ftplib
from datetime import datetime, timedelta
from time import sleep

INTERVAL = timedelta(minutes=10)

def do_something():
    print('doing it')
    # ftp file


last = None
while True:
    if not last:
        do_something()
        last = datetime.now()
        continue

    diff = datetime.now() - last
    if diff < INTERVAL:
        sleep(1)
        continue

    do_something()
    last = datetime.now()
abdusco
  • 9,700
  • 2
  • 27
  • 44