1

My current python script:

import ftplib
import hashlib
import httplib
import pytz
from datetime import datetime
import urllib
from pytz import timezone
import os.path, time
import glob

def ftphttp():
 ts = os.path.getmtime('Desktop/images/frame00.png') 
 dt = datetime.fromtimestamp(ts, pytz.utc)

 timeZone= timezone('Asia/Singapore')
 #converting the timestamp in ISOdatetime format
 localtime = dt.astimezone(timeZone).isoformat()

 cam = "002"
 lscam = localtime + cam
 ftp = ftplib.FTP('localhost','kevin403','S$ip1234')
 ftp.cwd('/var/www/html/image')

 m=hashlib.md5()
 m.update(lscam)
 dd=m.hexdigest()

 for image in glob.glob(os.path.join('Desktop/images/frame**.png')):
  with open(image, 'rb') as file:
   ftp.storbinary('STOR '+dd+ '.png', file)

 x = httplib.HTTPConnection('localhost', 8086)
 x.connect()
 f = {'ts' : localtime}
 x.request('GET','/camera/store?cam='+cam+'&'+urllib.urlencode(f)+'&fn='+dd)
 y = x.getresponse()
 z=y.read()
 x.close()
 ftp.quit()

As right now this line of code is only get one file timestamp:

ts = os.path.getmtime('Desktop/images/frame00.png'). 

But what if i send multiple file from a folder and get all the files timestamp. Is it possible to do it? I using ftplib to send multiple from a folder to another folder.

Alvin Wee
  • 195
  • 2
  • 4
  • 16
  • Use list comprehension? `tss = map(os.path.getmtime, ["Desktop/images/frame01.png", "Desktop/images/frame02.png"])` – Nehal J Wani Aug 14 '16 at 15:23
  • @NehalJWani Is it possible if using loop? – Alvin Wee Aug 14 '16 at 15:41
  • @Alvin, if I understand you correctly you can store all the files with any extension of your choice from the folder in list (for your reference: http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-in-python), for e.g. `files = [r'file_Path', r'filePath', r'filePath']`, then you can loop through each *filePath* from the list like: `for each in files: ts = os.path.getmtime(each)`, then using the *ts* value you can continue with your further logic. Hope it helps – justjais Aug 15 '16 at 15:23

1 Answers1

0

You could just walk the directory and getmtime for all the files in that directory if the directory in question is Desktop/images

You can replace

ts = os.path.getmtime('Desktop/images/frame00.png') 
dt = datetime.fromtimestamp(ts, pytz.utc)

With something like:

dirToCheck = 'Desktop/images'
for root, _, file in os.walk(dirToCheck):
    fileToCheck = os.path.join(root, file)
    ts = os.path.getmtime(fileToCheck) 
    dt = datetime.fromtimestamp(ts, pytz.utc)
    print ts, dt

With this approach you will need to give the absolute path to the images directory and also maybe store the timestampes in a list or dictionary with file - timestamp.

If you want to do it for png only you can also add a line like:

if file.endswith(".png"): 

before the fileToCheck line.

This should do it unless i misunderstood your question.

Bogdan
  • 1