Why copy only one statement when using pyperclip

Viewed 56

I tried to create a find email addresses in a text file it work pretty well.After using pyperclip I can copy only last email address but I want to copy all the emails to clipboard.How can I do this.And also I would like to try another way of do this if you better way to do this thing please let me know as well.By the way I have another text file that contains text file with emails.And I use this gitlist to make this python program gitlist link

from optparse 
import OptionParser

import os.path

import re

import pyperclip


regex = re.compile(("([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`"

                    "{|}~-]+)*(@|\sat\s)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(\.|"

                    "\sdot\s))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"))


def file_to_str(filename):

    """Returns the contents of filename as a string."""

    with open(filename) as f:

        return f.read().lower() # Case is lowered to prevent regex mismatches.


def get_emails(s):

    """Returns an iterator of matched emails found in string s."""

    # Removing lines that start with '//' because the regular expression

    # mistakenly matches patterns like 'http://foo@bar.com' as '//foo@bar.com'.

    return (email[0] for email in re.findall(regex, s) if not email[0].startswith('//'))


if __name__ == '__main__':

    parser = OptionParser(usage="Usage: python %prog [FILE]...")

    # No options added yet. Add them here if you ever need them.

    options, args = parser.parse_args()


    if not args:

        parser.print_usage()

        exit(1)


    for arg in args:

        if os.path.isfile(arg):

            for email in get_emails(file_to_str(arg)):

                print(email)

                pyperclip.copy(email)

        else:

            print('"{}" is not a file.'.format(arg))

            parser.print_usage()
1 Answers

I found a way to do this you don't need to write many lines as you did try this

with open(r"text.txt", "r") as txt_reader:
found_email = "Not Found"

for line in txt_reader:
    if "email" in line.lower():
        #Breaks line into a list of words seperated by ':' Choses the 2nd 
     word and removes blank spaces        
        found_email = line.split(":")[1].strip()

print(found_email)

as a example if your test.txt file is like this

Email: susantha@gmail.com

Password: pas5w0rd!

then this program will copy the email and password to clipboard

Related