How to get value from the first line of a Text (txt) file?

-1

So ... As you can see, the variable target defines the email that the message will be sent to.

However, I can not add the value of the first line of the list.txt within the variable target .

The idea of a broader view is that the script sends the emails, line by line, or else it adds the value of the first line, and at the end of the loop it removes that value from the list.txt and the variable so you can add a new value.

How to add the value of the first line of a text.txt file within a variable?

(and then remove the first line from the txt file, to continue the commit cycle .. until the list is finished. for i in xrange(texto): )

# -*- coding: UTF-8 -*-
import smtplib
import datetime
import random
import getpass
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class spammer():
def __init__(self):
    self.banner()
    self.spam()

def banner(self):
    print """                                                          

    By Mandrake Venom 2018
                                                      """

arq = open('./lista.txt', 'r')
texto = len(arq.readlines()) -1
valor = "" 
print "[+] Total de Emails [+] "
print "========================="
print texto 
print "========================="
print valor
arq.close()

def spam(self):
    # Credentials
    username = raw_input("Login [gmail]: ")
    password = raw_input("Senha: ")
    target = raw_input("Enviar email para: ")
    assunto = raw_input("Assunto: ")
    men = input("Body: ")

    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    try: server.login(username, password)
    except: print "[-] Authentication Error" ; exit()

    print "[!] Preparando para Enviar "

    try:
        for i in xrange(texto):             
            subj = assunto,  random.randrange(0,9999999999999)
            content = MIMEText(men, 'html')
            name = "fulano"
            date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )             
            msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % (name, target, subj, date, content)
            server.sendmail(username, target, msg)
    except smtplib.SMTPException:
            print "[-] An Error Occured During Process | " 
            print "[!] The target email might be wrong [Error] ===> ",target 
            exit()
    server.quit()
    print "[+]", target, " [Enviado] Continue ===>"

try:
spammer()
except KeyboardInterrupt:
print "\n[!] Tchau! byeee byeeeeee.... "
exit()
    
asked by anonymous 05.12.2017 / 20:30

1 answer

3

Try this:

with open('lista.txt', 'r') as f:
    primeira_linha = f.readline()

The with will use the file and close and readline will read the first line.

    
05.12.2017 / 20:49