Mnenomic Password Generator
user is redundant in your code, it's instantiated but never used at all so it should be removed. Another more wasteful redundancy is printing all the time, that actually slows down the code a surprising amount. You should strip them out. At most print once per loop and for exceptional circumstances (like errors).

Instead of checking if the file is empty it would be better test the lines in it and then afterwards write the password to the file. This way you write the password in the same way whether or not the file is empty, as it will just immediately find that the password isn't in the file anyway.

For the main body, you're reading in all the lines, then looping backwards over them by index to test them backwards. Is there a reason you're doing it backwards? I can't tell why you would do that. There's a lot of problems here, I would actually just start from scratch.

So, you need to test whether a password exists in the file, right? I'd make this a function first of all. So the function needs to take the password as an argument.

def exists(password):


Now we need to open the file and read each line in the file. Rather than read all the lines up front, it's much easier to read lines one by one. Python makes this incredibly easy, letting you iterate directly through the file, returning one line at a time.

    with open("MnemPass.txt") as f:
        for line in f:


Now, we can just test if the password is there and return True if it does, as this indicates that the password already exists.

            if password == line.strip():
                return True


I added .strip() to remove whitespace that might mess with the test. And then of course, if the whole file is read without this problem then you eventually reach the end of the function where you return False, indicating that it does not exist.

def exists(password):
    with open("MnemPass.txt") as f:
        for line in f:
            if password == line.strip():
                return True
    return False


Now. when we go back to the original code this makes things immensely tidier:

for mainLoop in range(passnum):
    newpass = generatePassword() # New password generated each iteration of loop
    if exists(newpass): #changed
        with open("Matchpassword.txt", "a") as passMatch:
            passMatch.write(newpass + "\n")
    else:
        with open("MnemPass.txt", "a") as storePass:
            storePass.write(newpass + "\n")


Now, this can be done even faster. If you were to go back to the function, you could use the any operator to lake the for loop and turn it into a generator expression. Generator expressions are basically like for loops condensed into a single expression. And if you pass one into any it can run a test on every element of a loop. It's faster than normal Python loops. So here's how it'd look:

def exists(password):
    with open("MnemPass.txt") as f:
        return any(password == line.strip() for line in f)


This means that if any ever finds a line that equals password then it will return True, but if it finds no such line it will return False.

After this, optimisation really ought to be done with storage. You could sort the passwords alphabetically and then intelligently detect where they should be. Maybe store in multiple files, corresponding to the starting character of the password. There's only so much that the exact code optimisation itself can do.