'How to make a file in a specific directory python
I would like to make a file in a specific "accounts" directory but I keep getting the error:
FileNotFoundError: [Errno 2] No such file or directory: '/accounts/Jason Silla.txt'
This is my code, so if anyone can explain what is happening. I am on the latest version of python on a Mac.
filePath = join("/accounts", username + ".txt")
newuser = open(filePath, "w")
newuser.write(username + "\n" + password)
Also I have seen the other posts about this but as you can see it is not working for me. I did import join from os.path so that is not the issue.
Solution 1:[1]
Check that there is an 'accounts' directory in the same directory where this python file you are running is located, if not just create i
Solution 2:[2]
Double check if your accounts directory exists, if not, create it.
try:
# remove leading '/' if you want to work in the current directory
os.makedirs("accounts")
except FileExistsError:
# directory already exists
newuser = open(filePath, "w")
# etc etc ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nestor Silva |
| Solution 2 |
