0

I have some code like:

username = input("Enter username:")

save_1 = open("save_1.txt", "a")
save_1.write((str(username)))
save_1.close()

When I try this code, it asks me for a username; but then when I put one in the window just closes and I cant find the save file. How can I make sure the file was created? Where will it be created?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • https://stackoverflow.com/questions/70086660/how-to-get-the-current-directory-in-python – Ignatius Reilly Aug 20 '22 at 22:38
  • ^ Is where the new file would end up. Probably next to the python file – Anthony L Aug 20 '22 at 22:40
  • You can use [os](https://docs.python.org/3/library/os.html), in particular [os.path](https://docs.python.org/3/library/os.path.html) to choose where to save your files. – Ignatius Reilly Aug 20 '22 at 22:41
  • Does this answer your question? [How do I create a file at a specific path?](https://stackoverflow.com/questions/5104957/how-do-i-create-a-file-at-a-specific-path) – Ignatius Reilly Aug 20 '22 at 22:44
  • @JamiuShaibu inline code blocks (with backticks) are for formatting actual code, not technical terms (and "rpg" doesn't qualify anyway). It's also [better to edit background out of the question](https://meta.stackoverflow.com/questions/343721), unless it's relevant to actually *understanding* the question. – Karl Knechtel Aug 22 '22 at 01:20

1 Answers1

0

The directory where you are running the python file should also be the place the file appears.

my_project
    my_directory
        main.py
        save_1.txt

But you might want to do this

username = input("Enter username:")

save_1 = open("/path/from/root/save_1.txt", "a")
save_1.write((str(username)))
save_1.close()

If this doesn't happen, try checking your python installation. If you need anything else, let me know!

SimplyDev
  • 90
  • 2
  • 11