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'
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)
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
Related
''''
from datetime import datetime
now = datetime.now().time()
print(now)
o/p: 21:44:22.612870
''''
But, when i am trying:
''''
import datetime
now = datetime.now().time()
print(now)
''''
it give following error:
Traceback (most recent call last):
File "D:/3. WorkSpace/3. Django/datemodel/first.py", line 9, in
now = datetime.now().time() # time object
AttributeError: module 'datetime' has no attribute 'now'
any one explain what is difference between both?
The datetime library exports a module called datetime.
Modules are Python .py files that consist of Python code. Any Python file can be referenced as a module.
if you want you can also use it this way:
import datetime
datetime.datetime.now()
import pytz
import datetime
timezone = pytz.timezone('Poland')
date = timezone.localize(datetime.datetime(2018, 10, 1))
pytz.timezone(date.tzname())
Unfortunately in Python 3.5.2, with it crashes with
Traceback (most recent call last):
File "timezones.py", line 6, in <module>
pytz.timezone(date.tzname())
File "/usr/local/lib/python3.5/dist-packages/pytz/__init__.py", line 178, in timezone
raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'CEST'
In one part of program timezone aware time object is created. In other part it is necessary to get timezone identifier back.
To avoid XY issues: I am calculating sunrise and sunset data using skyfield library. To do this I need to pass timezone as one of parameters.
From what I see at https://docs.python.org/3/library/datetime.html there is a timezone but for setting timezone, not getting it.
The TZ database timezone name is stored as the zone attribute of the zone object returned by pytz.timezone():
>>> import pytz
>>> import datetime
>>>
>>> timezone = pytz.timezone('Poland')
>>> date = timezone.localize(datetime.datetime(2018, 10, 1))
>>> date.tzinfo.zone
'Poland'
As you can see, the zone object itself is available as date.tzinfo after calling localize(), so you can just use that directly instead of passing the name back into pytz.timezone() if that's why you need it.
So I'm writing a program with
import datetime
import time
I'm using time to record the time it takes the program to run, and I need to check the date so if the file is more than a certain age, don't process it.
I keep getting this error when trying to use these two classes
Traceback (most recent call last):
File "<stdin>", line 563, in <module>
File "<stdin>", line 498, in main
AttributeError: type object 'datetime.time' has no attribute 'time'
shell returned 1
Is it not possible to use both time and datetime in one program?
Some of the code:
import PyPDF2
import re
import os
#Time testing
import time
#Using this to check if address is in proper format and to clean it up
import usaddress
#testing this one out
import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import *
#Timer starts
start_time = time.time() #Error is referring to this line, line 498
#Opens 3 different files
#For file in folder parse it to text
#Writes some things to file
#Gets the date from the file
if date != None:
fileDate = parse(date).year
now = datetime.now()
print now.year, now.month, now.day
#Ends the timer and prints the time to the console
print("--- %s seconds ---" % round(time.time() - start_time, 2))
Here is your problem:
import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import * # <<<< problems
First you are importing datetime and then you are importing everything from datetime.
Be explicit, and only import what you need.
from datetime import datetime
Then you can use it as datetime.now or whatever methods you may need.
As a rule of thumb, never import *. It causes exactly these sorts of issues.
The problems is:
from datetime import *
because it imports time from datetime. It's always better to import only what you need. But if you also really need this method you could do (for example):
from datetime import time as dt
That's why import * is dangerous...
When I input the simple code:
import datetime
datetime.utcnow()
, I was given error message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
datetime.utcnow()
AttributeError: 'module' object has no attribute 'utcnow'
But python's document of utcnow is just here: https://docs.python.org/library/datetime.html#datetime.datetime.utcnow. Why does utcnow not work in my computer? Thank you!
You are confusing the module with the type.
Use either:
import datetime
datetime.datetime.utcnow()
or use:
from datetime import datetime
datetime.utcnow()
e.g. either reference the datetime type in the datetime module, or import that type into your namespace from the module. If you use the latter form and need other types from that module, don't forget to import those too:
from datetime import date, datetime, timedelta
Demo of the first form:
>>> import datetime
>>> datetime
<module 'datetime' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/lib-dynload/datetime.so'>
>>> datetime.datetime
<type 'datetime.datetime'>
>>> datetime.datetime.utcnow()
datetime.datetime(2013, 10, 4, 23, 27, 14, 678151)
I am trying to initialize a time object like this:
t = datetime.time(0,0,0)
but I am getting this error:
descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
I have these things imported
import datetime
from datetime import datetime, date, time
import time
They seem a bit redundant so I am wondering if this is what is causing the problem
I am also using the strptime method and the combine method
earliest = datetime.combine(earliest, t)
value = datetime.strptime(value, format)
You can create the object without any values:
>>> import datetime
>>> datetime.time()
datetime.time(0, 0)
You, however, imported the class datetime from the module, replacing the module itself:
>>> from datetime import datetime
>>> datetime.time
<method 'time' of 'datetime.datetime' objects>
and that has a different signature:
>>> datetime.time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument
>>> datetime.time(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'
Either import the whole module, or import the contained classes, but don't mix and match. Stick to:
import datetime
import time
if you need both modules.
The constructor for time is:
class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])
(from http://docs.python.org/library/datetime.html#time-objects)
This works for me:
In [1]: import datetime
In [2]: t = datetime.time(0, 0, 0)
In [3]: print t
00:00:00
It's the fact that you're importing a conflicting datetime from datetime. You probably meant time, except you're also importing a conflicting time. So how about:
import datetime as dt
and
t = dt.time(0, 0, 0)