-4

My 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():
 files = glob.glob('Desktop/images/*.png')
 ts = map(os.path.getmtime, files)
 dts = map(datetime.fromtimestamp, ts)
 print ts

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

I was trying to get the multiple files timestamp. I able to print out all the files in my folder

 [1467910949.379998, 1466578005.0, 1466528946.0]

But it also prompt me this error about the timezone. Anybody got any ideas?

Traceback (most recent call last):
 File "<pyshell#76>", line 1, in <module>
 ftphttp()
File "/home/kevin403/Testtimeloop.py", line 22, in ftphttp
 localtime = dts.astimezone(timeZone).isoformat()
AttributeError: 'list' object has no attribute 'astimezone'
Alvin Wee
  • 195
  • 2
  • 4
  • 16

2 Answers2

1

You are trying to call a method on a list of objects, instead of the objects in the list. Try calling the method on the first object instead:

localtime = dts[0].astimezone(timeZone).isoformat()

Or map over the list to get all timestamps in iso format:

localtimes = map(lambda x: x.astimezone(timeZone).isoformat(), dts)
Filip Haglund
  • 13,919
  • 13
  • 64
  • 113
  • I tried but i got this error: ValueError: astimezone() cannot be applied to a naive datetime – Alvin Wee Aug 15 '16 at 06:10
  • @AlvinWee that's another question, which seems to have an answer here https://stackoverflow.com/questions/12626045/pytz-and-astimezone-cannot-be-applied-to-a-naive-datetime – Filip Haglund Aug 15 '16 at 06:12
0

dts is a list of time zones. So you need to do:

[ts.astimezone(timeZone) for ts in dts]

This will give you a list of the three time zones

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35