简体   繁体   English

TypeError:需要浮点数PYTHON

[英]TypeError: a float is required PYTHON

My current python script: 我当前的python脚本:

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():
 dataset_path='Desktop'   
 files = glob.glob(dataset_path+"/images/*.png")   
 ts = files.sort(key=os.path.getmtime)
 dt = datetime.fromtimestamp(ts, pytz.utc)
 timeZone= timezone('Asia/Singapore')
 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/*.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()

The trackback: 引用:

Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
 ftphttp()
File "/home/kevin403/Testtimeloop.py", line 17, in ftphttp
 dt = datetime.fromtimestamp(ts, pytz.utc)
TypeError: a float is required

I trying to get a list of files in a folder to get their modified timestamp and store it in a database. 我试图获取文件夹中文件的列表,以获取其修改后的时间戳并将其存储在数据库中。 But i can't seem to do it. 但是我似乎做不到。 Anybody got an idea on how to do it? 有人知道怎么做吗? Been trying it for quite long and i'm new to python. 尝试了很长时间,我是python的新手。

This occurs because the function glob.glob, returns an array of strings, and you are trying to pass this result to "datetime.fromtimestamp" function, that expects a number. 发生这种情况是因为函数glob.glob返回一个字符串数组,并且您试图将此结果传递给需要一个数字的“ datetime.fromtimestamp”函数。

Any moment you "store" the modified date, to use after. 您“存储”修改日期的任何时刻,都可以在此之后使用。

You needs to manipulate the files one by one. 您需要一个一个地操作文件。 Example (I didn't test): 示例(我没有测试):

files = glob.glob(dataset_path+"/images/*.png")   
ts = files.sort(key=os.path.getmtime)
for file in ts:
    ms = os.path.getmtime(file)
    dt = datetime.fromtimestamp(ms)
...

or if you only needs the modification dates (without the path of file): 或者,如果您只需要修改日期(没有文件路径):

files = glob.glob(dataset_path+"/images/*.png")
ts = map(os.path.getmtime, files)
dts = map(datetime.fromtimestamp, ts)
...

References: 参考文献:

https://docs.python.org/2/library/datetime.html#datetime.date.fromtimestamp https://docs.python.org/2/library/glob.html#glob.glob https://docs.python.org/2/library/datetime.html#datetime.date.fromtimestamp https://docs.python.org/2/library/glob.html#glob.glob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM