• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED [help] python msg = 'blah %var?'

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

wagex

Chapstick Eating Premium Member
Joined
Jan 14, 2011
ok so heres my script i found something some one cooked up to generate a future date in time depending on a certain number of days.

for instance this email will be sending on friday reminding people to turn in paperwork for the week ending in "sundays date" two days in the future.
i cant figure out how to put a string in the message if i put a %x for instance or even %m/%d/%y it just shows exactly like that.
Code:
import datetime
now = datetime.datetime.now()
print now
from datetime import timedelta
diff = datetime.timedelta(days=5)
print diff

print now + diff

future = now + diff
future.strftime("%m/%d/%Y")
'06/02/2008'
import smtplib
fromaddr = '[email protected]'
toaddrs  = '58036*****@email.uscc.net'
msg = 'please be sure to turn in inspection reports the week ending in ?%var?'
username = '[email protected]'
password = 'p***rd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
 
I don't know Python, but looking at the specs, you use "+" character concatenation. So, you'd do something like this:

Code:
msg = 'please be sure to turn in inspection reports the week ending in ' + var
 
Last edited:
welp thideras pointed me towards a page that explained how to put strings inside of a string hence the str() then it took me a while to put two and two together i had to call the future.strftime("%m/%d/%Y") as a name instead of a string inside the string so i named it date and str(date) worked :) and to change the format i just have to change the format at the end of strftime
this is what i ended up with after cleaning it up some

the only reason why this works for sundays date every week obviously because it sends on friday lol.

Code:
import smtplib
import datetime
now = datetime.datetime.now()
print now
from datetime import timedelta
diff = datetime.timedelta(days=2)
print diff
print now + diff
future = now + diff
future.strftime("%m/%d/%Y")
date = future.strftime("%m/%d/%Y")
fromaddr = '[email protected]'
toaddrs  = '58036*****@email.uscc.net'
msg = ("please be sure to turn in inspection reports for the week ending on " + str(date))
username = '[email protected]'
password = 'p***word'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
 
in python if you are putting a string or another variable into a string it would look like this

Code:
#!/usr/bin/python
import os
import sys

user_name = sys.argv[1]
date = sys.argv[2]
message = "hello my name is %s and the date is %s" % (user_name, date)

print message

This will produce the following:

Code:
stratus@stratus-laptop ~/temp $ ./test.py Stratus `date +%F `
hello my name is Stratus and the date is 2013-11-03

Is that what you were asking?
 
umm idk lol. the last bit of code i posted is what worked that will send an email with the text and then end with a date as a variable. the date is also two days ahead of when the text is sent for instance i have it set to run on friday afternoon, so i put two days in the future so it will post sundays date in the message because our work weeks end on a sunday.

edit now i understand what you were doing i like that way better i think :) il try that too sems pretty simple.
 
Python 2 style string formatting (using the % symbol) is going away. It's what I'm most used to, and I like its elegance, but you should probably consider the Python3 way, which will still work in current Python2 releases:

Code:
msg = "please be sure to turn in inspection reports for the week ending on {}".format(str(date))

See http://docs.python.org/3.1/library/string.html#formatspec for more info on how that formatting should work.
 
I actually was not aware of the shift.

I do almost all my python work in CentOS, which will probably not be moving to python 3 at least until CentOS 7.

Confirmed the {}.format(variable) works in python 2

I am going to have to pull out my python 3 book which has been mostly gathering dust to this point
 
hmm this is interesting guess theres more than one way to skin a cat :) thanks for the tips guys! il keep this in mine as im going to work on another piece of it later today that will be a variable depending on the phone number so it will add their name at the beginning of the message pullling from a txt document or something like that.
 
Back