我正试图在一个文件夹中创建一个.txt文件,该文件不是脚本正在运行的目录。我有一个脚本所在的文件夹,我可以在脚本所在的同一目录中创建文件夹,但不会在该文件夹中创建文本文件。我通常两个错误之一运行:PermissionError: [Errno 13] Permission denied
或FileNotFoundError: [Errno 2] No such file or directory:
如何创建文本文件在使用python的文件夹中?
这是一个密码管理器顺便说一句,防止五人告诉我,这不是安全的,我知道这一点,这个项目是纯粹的教育和我始终使用占位符。
有这种类似的问题,但他们都对Java或C++ ...
这里是我的代码:
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()
如果您对我的代码有任何疑问,请随时问!
当我尝试这样做,它给了我这样的:回溯(最近最后一次通话): 文件 “keeper.py”,第47行,在 的main() 文件 “keeper.py” 23行,主 huehuehue = abc123.read()。strip()。split() io.UnsupportedOperation:不可读 –
您可能必须确保文件存在,然后在'r +'模式下打开它。据我所知,您无法以'a'或'w'模式读取文件,忘记提及这一点。 –
当我在w模式下打开它时,出现同样的错误,就像我在模式中打开它时一样。它虽然创建文件...它只是不创建该文件夹中的文件。 –