Search code examples
pythondirectory

How can i specify the directory in which i want my txt file to be created with python?


I'm currently working on a project on python. In my program i ask the user to input some informations that will be then stored in his document.

username = input("Input your username")
w = input("Do you want to view or edit your file?(v/e) ")
    if w == "v":
        open(username + ".txt", "r", "Serax/desktop/Project/Usernames")
    elif w == "e":
        a = open(username + "txt", "a", "Serax/desktop/Project/Usernames")

I have manually created a directory in which i want to store all the users document (name of the directory is "Usernames") it is contained in the "Project" directory.The "project" directory contains the "crypto.py". document and the "Usernames" directory. I have found out ways to create new directories but i can't find the way to specify the location of my document. In the complete program i have imported os, i. don't know if it could help.


Solution

  • You can just resolve to the absolute path like foo/bar.txt where foo is the child directory in current directory, and use backslashes for windows.