日期时间和时间不能在同一程序中使用?

时间:2017-10-24 13:22:12

标签: python python-2.7 datetime time

所以我正在用

编写一个程序
import datetime
import time

我正在用时间记录程序运行所需的时间,我需要检查日期,如果文件超过一定年龄,请不要处理它。

尝试使用这两个类时,我一直收到此错误

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

是否无法在一个程序中同时使用时间和日期时间?

部分代码:

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))

2 个答案:

答案 0 :(得分:4)

这是你的问题:

import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import *  # <<<< problems

首先导入datetime,然后从datetime导入所有内容。

明确,只导入你需要的东西。

from datetime import datetime

然后您可以将其用作datetime.now或您可能需要的任何方法。

根据经验,永远不要导入*。它确实导致了这些问题。

答案 1 :(得分:0)

问题是:

from datetime import *

因为它从datetime导入时间。最好只导入您需要的东西。但是如果你真的需要这种方法,你可以做(​​例如):

from datetime import time as dt

这就是导入*危险的原因......