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.
CodePudding user response:
You probably don't want to refer to your directory as "/accounts" but instead refer to it as "accounts". When you use a leading slash / at the beginning of a file name, then you are referring to a directory relative to the root of your entire file storage tree. If there is not leading slash, then you are referring to a directory relative to the current local directory where your script is being run.
CodePudding user response:
Double check if your accounts directory exists, if not, create it.
try:
# remove leading '/' if you want the file to be in the current directory
os.makedirs("accounts")
except FileExistsError:
# directory already exists
newuser = open(filePath, "w")
# etc etc ...
CodePudding user response:
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
