How to concatenate string and float?

Asked by Jeff Sant

Guys,

I need to find out what is the date and time of the file uploaded on server, i am trying to get it from a lot of techniques but getting error, please help me.

time.mktime(u.info().getdate("date"))

i can not print or popup it, have an error that can not concatenate string and float.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Jeff Sant
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

This error message tells you, that you want to concatenate a string with a float ;-)

time.mktime(u.info().getdate("date"))
cannot be the cause, since there is no concatenation here. Must be either in info() or in getdate().

If you have identified the float object in a concatenation, just cast it to a string using str(float_object).

Revision history for this message
RaiMan (raimund-hocke) said :
#2

if you are trying to say:

print "some text"+time.mktime(u.info().getdate("date"))

then you have to say either:
print "some text", time.mktime(u.info().getdate("date"))

or
print "some text"+str(time.mktime(u.info().getdate("date")))

or
print "some text %d"%(time.mktime(u.info().getdate("date")))

same for popup()

Revision history for this message
Jeff Sant (sant-jeff) said :
#3

Raiman,

I tried it and i worked, but it is not showing me date instead it has some value such as 1312080019.0

How can i get it in the format of date and time, so that i can verify if the file available online is newer or not.

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#4

the information you need: http://docs.python.org/release/2.5.2/lib/module-time.html

You might be looking for time.localtime().

Revision history for this message
Jeff Sant (sant-jeff) said :
#5

Actually, i don't want local time, my code is following

u = urllib.urlopen("http://abc.com/xyz.zip")
popup("some text "+str(time.mktime(u.info().getdate("date"))))

I want to compare the date and time of the file with a date and time in a variable so that i could know if the file online is newer than my version or not?

Revision history for this message
RaiMan (raimund-hocke) said :
#6

did you look at the docs?

did you try:
print localtime(time.mktime(u.info().getdate("date")))

and look wether the returned content might help?

Revision history for this message
Jeff Sant (sant-jeff) said :
#7

Yes i have looked into docs, and used the command you mentioned, but i get error that localtime is not defined

import urllib
import time
import sys

u = urllib.urlopen("http://abc.com/xyz.zip")
popup(localtime(time.mktime(u.info().getdate("date"))))

NameError: name 'localtime' is not defined

Revision history for this message
RaiMan (raimund-hocke) said :
#8

ok, sorry for misleading or I could say: Jeff, test not passed ;-)

Python basics:
u = urllib.urlopen("http://abc.com/xyz.zip")
popup(time.localtime(time.mktime(u.info().getdate("date"))))

--- methods of modules imported with "import some_module" have to be qualified with module name: some_module.method()

BTW:
import time
import sys

are not needed, since already imported by Sikuli script initialization

Revision history for this message
Jeff Sant (sant-jeff) said :
#9

Dear Raiman,

Thanks, but it didn't resolve my issue, i have modified the code

t=time.strftime("%Y-%m-%d %H:%M", time.localtime(time.mktime(u.info().getdate("date"))))
popup("%s"%t)

But instead of getting the time of the file available on a website, it is changing with my system time.

Is there any possible solution where i can get the date and time of the file located on a web server or a website?

Revision history for this message
RaiMan (raimund-hocke) said :
#10

I do not know, where you got this code, but when having problems like this it is a good idea to evaluate the return values step by step, before building long chains of dot-concats.

u.info() returns a dictionary with header values. The value you are looking for is "last-modified".
"date" contains the current date/time (time of request).

so this does what you want:

lastMod = time.strftime("%Y-%m-%d",u.info().getdate("last-modified"))
popup("file last modified at: %s"%lastMod)

Revision history for this message
Jeff Sant (sant-jeff) said :
#11

Dear Raiman,

This resolved my issue, thanks... You are the champ!