0

I am trying to create a .txt file in a folder which is not the directory the script is being run from. I have a folder where the script is, and I am able to create the folder in the same directory the script is in but It won't create the text file in the folder. I usually run in to one of two errors: PermissionError: [Errno 13] Permission deniedor, FileNotFoundError: [Errno 2] No such file or directory:

This is for a password keeper btw, and to prevent five people telling me it's not secure, I am aware of this, this project is purely educational and I always use placeholders.

There are similar questions to this but they are all for java or c++...

Here is my code:

def main():
    import os
    import os.path
    abc = open("userpassfile.txt", "r+")
    userpassfile = abc.read().strip().split()
    actCheck = input('Do you already have an account?')
    if (actCheck == 'Yes' or actCheck == 'yes'):
        loginUser = input('What is your username?')
        loginPass = input('What is yout password?')
        if (loginUser and loginPass in userpassfile):
            dirCheck = input('Account Settings? [y/n]')
            if (dirCheck == 'y' or dirCheck == 'Y'):
                print('This function is not working yet!')
                addpassCheck = input('Would you like to add a password?')
                if (addpassCheck == 'yes' or addpassCheck == 'Yes'):
                    abc123 = open(loginUser + '.txt', "r+")
                    huehuehue = abc123.read().strip().split()
                    addpass1 = input('What service is the pass')
                    addPass2 = input('What is the password')
                    abc123.write('(' + addpass1 + ',' + addPass2 + ')' + '\n')

                else:
                    print('hihi')
            else:
                print("hehe")
        else:
            print('Incorrect password or username!')
    else:
        createAct = input('would you like to create one?')
        if (createAct == 'Yes' or createAct == 'yes'):
            save_path = 'C:/Users/Ari Madian/Desktop/Scripts/Python Scripts/Python Password Project'
            createUser = input('What would you like your username to be?:')
            createPass = input('What would you like your password to be?:')
            abc.write(createUser + '\n')
            abc.write(createPass + '\n')
            os.makedirs(createUser)
            completeName = os.path.join(save_path, createUser + ".txt")



main()

If you have any questions about my code, feel free to ask!

2 Answers 2

2

Try to open the .txt file in a mode or w mode. If you open it in r+ mode the file will not be created since r+ does not create a file.

'a' creates the file if it does not exist but if it does exist it simply adds to it. 'w', on the other hand, deletes the existing one and creates a new one. I think you want to use a here.

EDIT: I misunderstood the question here. The file was being created in the wrong directory. Just for future reference, if putting a file in a subdirectory, make sure to add "/" when seperating them.

Sign up to request clarification or add additional context in comments.

10 Comments

When I try to do this, it gives me this: Traceback (most recent call last): File "keeper.py", line 47, in <module> main() File "keeper.py", line 23, in main huehuehue = abc123.read().strip().split() io.UnsupportedOperation: not readable
You probably have to make sure that the file exists and then open it in r+ mode. You can't read a file in a or w mode to my knowledge, forgot to mention that.
I get the same error when I open it in w mode as I do when I open it in a mode. It creates the file though... It just doesn't create the file in the folder.
Did you already change the code to reflect checking if the file exists first? Also, I would advise using with open(file, extension) as varname as with automatically closes files for you.
I feel like we got a bit off topic and that was probably my fault... It creates the txt file no problem but not in the folder i want it to be created in.
|
0

The import os and import os.path need to be at the beginning of the program. For example:

import os
import os.path
def main():

Rather than inside the main(): function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.