U
U
Uno di Palermo2018-03-06 12:20:13
Python
Uno di Palermo, 2018-03-06 12:20:13

readline() method - how to read random lines from a txt file?

Hello, there is a script, it sends a soap and makes a header from 85 characters of a file line.
starting from the first.
how to make him choose random, out of order?

import smtplib
import random
from smtplib import SMTP_SSL
import email
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
import sys, traceback
import time
from time import gmtime, strftime
 
c = 0
files = [
  'C://Users//test//Downloads//1_Amanda-De-Cadenet.jpg',
  'C://Users//test//Downloads//0_a82a7_7db42dad_orig.jpg',
  'C://Users//test//Downloads//GildaAmbrosio-1.jpg',
  'C://Users//test//Downloads//gilda20140223-4844-2.jpg'
]

answers = ['y', 'да', 'YES', 'yes', 'Y']
sender_address = '[email protected]' 
receiver_address = '[email protected]' 

#открываем файл
f = open("C:\\Users\\test\\Desktop\\spam.txt")

def send(): 
    global c         
    date = strftime("%H:%M:%S")
   
   try:
        while True:
            
            for i, filename in enumerate(files):
                path = random.choice(files)
                    
                if not os.path.isfile(path):
                    print("Неверный путь к файлу")
                    continue
                else:
                    filename = os.path.basename(path)
                         
                    basename = os.path.basename(files[i])
                    #Receiver email

                    sender_address = '[email protected]'
                           
                    #Compose attachment
                    part = MIMEBase('application', "octet-stream")
                    part.set_payload(open(files[i], "rb").read())
                    encoders.encode_base64(part)
                    part.add_header('Content-Disposition', 'attachment; filename = "%s"' % basename)
                                
                    #Compose message
                    msg = MIMEMultipart()
                    #Header \\ заголовок письма    
                    msg['Subject'] = f.readline()[0:85] #строка создающая заголовок
                    msg['From'] = sender_address
                    msg['To'] = receiver_address
                    msg.attach(part)
                    
                    #Send mail
                    smtp = smtplib.SMTP('smtp.mail.ru', 25)
                    smtp.ehlo()
                    smtp.starttls()
                    
                    smtp.login(sender_address, 'test')
                    smtp.sendmail(sender_address, receiver_address, msg.as_string())
                    c += 1
                    print('%s sent to: %s ' % (filename, receiver_address), c, '\n')
    except Exception:
        print(traceback.format_exception(*sys.exc_info())[1])
        input("End ")


def sendFileByFile():
    for i, filename in enumerate(files):
        path = filename
        filename = os.path.basename(path)

    global c

    global sender_address
    
    r_address = input("спамить на мыло: %s\nфайлом %s? --->>> " % (receiver_address, filename))
    if r_address in answers: 
        send() 
    if not r_address:
        send()

sendFileByFile()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DDDsa, 2018-03-06
@DDDsa

instead of
write

msg['Subject'] = random.choice(f.readlines())[:85] #строка создающая заголовок

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question