0

My python script:

import ftplib
import hashlib
import httplib
import pytz
import datetime
import urllib
import os
import glob

def ftphttp():
 localtime = datetime.datetime.now(tz=pytz.utc).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/*.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()

I wanted to get the time from the file properties because i do not want it from the time that i send. Anybody can help me with this?

Alvin Wee
  • 195
  • 2
  • 4
  • 16

1 Answers1

0

@Alvin, You cannot get ISOdatetime from getmtime directly, if you want to convert the output date from getmtime, you can do it in following manner:

import os.path, time
from datetime import datetime
from pytz import timezone
import pytz

ts = os.path.getmtime(file_path) 
dt = datetime.fromtimestamp(ts, pytz.utc)

timeZone= timezone('supported_timezone')
#converting the timestamp in ISOdatetime format
dt.astimezone(timeZone).isoformat()

Output e.g.: '2016-02-16T23:32:06.260088-05:00' for 'US/Eastern' timezone

justjais
  • 344
  • 3
  • 13
  • But i wanted this isodatetime format: 2016-02-16T23:32:06.260088Z – Alvin Wee Aug 11 '16 at 12:40
  • Can you let me know that in '260088Z' what you are trying to refer by **'Z'**? – justjais Aug 11 '16 at 15:20
  • Because my web service Isodatetime require to have the 'Z' in order to store into the database – Alvin Wee Aug 12 '16 at 03:20
  • Thanks it works, i just encode the isodatetime for the output then can store already. – Alvin Wee Aug 12 '16 at 04:03
  • But do you know how to store different file with their own timestamp with getmtime? – Alvin Wee Aug 12 '16 at 06:06
  • instead of one file like this : ts = os.path.getmtime('Desktop/images/test0.png'). because i got multiple file that consist of test0.png - test3.png – Alvin Wee Aug 12 '16 at 06:08
  • @AlvinWee I didn't got your scenario, but if you want to store file with file timestamp, you can create a new file with timestamp name and with any extension then write to the file and store(for reference you can check following link: http://stackoverflow.com/questions/8024248/telling-python-to-save-a-txt-file-to-a-certain-directory-on-windows-and-mac). – justjais Aug 12 '16 at 08:34
  • As right now this line of code is only get one file timestamp: ts = os.path.getmtime('Desktop/images/test0.png'). But what if i send multiple file from a folder and get all the file timestamp. Is it possible to do it? – Alvin Wee Aug 13 '16 at 02:35
  • @AlvinWee, if you have multiple files, you can store all the files from the folder to python list and using python for loop you can loop through each file and get the *os.path.getmtime()* for each of the respective file, then you can write the logic of storing the filename with the timestamp naming pattern. – justjais Aug 13 '16 at 07:52
  • can you help me with this? Trying to do this too. – Alvin Wee Aug 14 '16 at 05:43