Demo entry 6166017

prova

   

Submitted by anonymous on Oct 07, 2016 at 02:33
Language: Python. Code size: 2.6 kB.

# BLIND TEST FILE NAMING
# This script copies the files in the input folder, renaming them with a random number. 
# It then saves the correspondes to file, that it is sent to the email address provided

###### Import relevant libraries to modify the files

import os, random, shutil

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders

import readline
readline.parse_and_bind("tab: complete")

from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read('pyEmailConf.ini')

###### Start the shuffling!

# Input folder and output folder from user
INPATH = input('Input folder: ');
OUTPATH = input('Output folder: ');

# check if saving directory exists, otherwise it creates one
if not os.path.exists(OUTPATH):			
	os.makedirs(OUTPATH)

# create the file where to save the correspondence between orinal files names and new names
file = open(OUTPATH+"/blindtest_reference.txt","w")		

filenames = sorted(os.listdir(INPATH))			# ordered list of the file in the input dir
total=len(filenames)					
numbers=random.sample(range(total),total)		# generate not repeated sample of random numbers
extension = os.path.splitext(filenames[0])[1]		# extract the extension of the files

# Copy a new file into the output folder renaming it accordingly to the random number generator 
for i in range(total):
	text=filenames[i] + "\t" + str(numbers[i])
	file.write(text + "\n")	
	shutil.copy(INPATH + "/" +filenames[i],OUTPATH +"/"+str(numbers[i])+extension) 

file.close()

print("All Blinded! \n")



##### Sending the reference file by email
fromaddr = config.get('userID','Address')
password = config.get('userID','Password')
toaddr = input('Send the reference file to: ')

msg = MIMEMultipart()
 
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = input('Subject: ')
 
body = input('Enter your message: ')
 
msg.attach(MIMEText(body, 'plain'))
 
filename = "blindtest_reference.txt"
attachment = open(OUTPATH+"/blindtest_reference.txt", "rb")
 
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
 
msg.attach(part)
 
servername = config.get('userID','Server')
server = smtplib.SMTP(servername, 587)
server.starttls()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

os.remove(OUTPATH+"/blindtest_reference.txt")

print("The Truth is safe!")

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).