Python Forum
Adding an ascending number [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding an ascending number [SOLVED]
#1
Hello everybody,

I have a mail script written in python and now I want to add an ascending number in my script like this:

emailContent = "Text 1" + i+1 + "Text 2"

This is meant to be the information how many mails I have received. Can anyone help with this? This is my script:

#!/usr/bin/env python3

import smtplib
import time

f1 = open("../index/mail.txt","r")
mail = f1.read() [:-1]

f2 = open("../index/passwd.txt","r")
passwd = f2.read() [:-1]

f3 = open("../index/receiver.txt","r")
receiver = f3.read() [:-1]

#Email Variables
SMTP_SERVER = 'smtp.gmail.com'  #Email Server (don't change!)
SMTP_PORT = 587                 #Server Port (don't change!)
GMAIL_USERNAME = mail           #change this to match your gmail account
GMAIL_PASSWORD = passwd         #change this to match your gmail password

class Emailer:
    def sendmail(self, recipient, subject, content):

        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)

        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()

        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

        #Send Email & Exit
        msg = (headers + "\r\n\r\n" + content).encode('utf-8')
        session.sendmail(GMAIL_USERNAME, recipient, msg)
        session.quit

sender = Emailer()

sendTo = receiver
emailSubject = "Debug"
emailContent = "Text 1" + i+1 + "Text 2"

#Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailConten$
sender.sendmail(sendTo, emailSubject, emailContent)
Reply
#2
I assume that you are wanting to concatenate the incremented variable i with two strings. Is this what you're looking for?

i = 9
emailContent = "Text 1 " + str (i+1) + " Text 2"
print (emailContent)
Reply
#3
This goes into the direction. But when executing the script it stays with 10. how do I rewrite i so that
i gets higher after every execution of the script ?
Reply
#4
IMHO sooner or later, when you least expect it, you will regret that you only open files but never close them.

F-strings (formatted string literals) are around appr 5 years so it could be good time to use them. Improves readability and eliminates need to convert datatypes.

If you need preserve counter between runs of program then you could write it into file and retrieve it from there.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 349 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,254 Jul-11-2022, 09:22 AM
Last Post: ibreeden
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,152 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  adding elements to a list that are more than a specific number Olavv 2 2,203 Mar-19-2020, 06:05 PM
Last Post: Olavv
  Adding elements to a list by number Olavv 4 2,946 Mar-08-2020, 11:16 AM
Last Post: ndc85430
  Sorting Arrays in Ascending vino689 8 3,534 Jan-12-2020, 07:42 AM
Last Post: vino689
  Adding markers to Folium map only adding last element. tantony 0 2,125 Oct-16-2019, 03:28 PM
Last Post: tantony
  naming images adding to number within multiple functions Bmart6969 0 1,926 Oct-09-2019, 10:11 PM
Last Post: Bmart6969
  Adding a line number to an lxml Element vindy 0 3,362 Mar-08-2019, 08:34 PM
Last Post: vindy
  Adding 1 to number JohnNo 5 4,547 Apr-29-2017, 02:31 PM
Last Post: Turry

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020