问题描述
我只是在使用 chmod 方法时遇到问题。我没有完全学习编程,但我有一些经验,我正在尝试制作一个脚本来跟踪我的小型家庭企业的收入和支出。
我认为问题在于写入文件的部分或 chmod 方法,两者都靠近底部:
zlib
我想我可能没有正确使用 os.chmod 方法?我仍然收到以下错误:
PermissionError: [Errno 13] 权限被拒绝: 'C:\Users\iarko\Desktop\PCE Business\Financials\Incomes'
请告诉我是什么问题,感谢您的阅读!
解决方法
主要问题出在您的行 with open(incomesPath,"a") as weeklyIncomeFile:
中,您尝试在其中打开文件等目录。
在 Python 3 中,我建议使用:
-
pathlib
用于路径 - f-strings 用于打印字符串中的值
而且变量名通常是小写的。
考虑到这些修改后,您的代码可能如下所示:
from datetime import datetime
from pathlib import Path
from subprocess import check_output
weekly_datetime = datetime.now()
weekly_date = weekly_datetime.strftime("%Y,%m,%V - Incomes")
incomes_path = Path("C:\\Users\\iarko\\Desktop\\PCE Business\\Financials\\Incomes")
income_file_path = incomes_path / f"{weekly_date}.txt"
income_file_path.touch()
# If you need to set the correct access rights,use "icacls in Windows"
check_output(
["icacls",str(income_file_path),"/inheritance:r","/grant:r",f"Everyone:F"]
)
making_choice = True
while making_choice == True:
print("1. Add another Income\n2. Stop adding incomes")
add_or_stop = input()
if add_or_stop == "2":
making_choice = False
if add_or_stop == "1":
print("Please enter the name of this income: ")
name = input()
print("Please enter the amount of this income: ")
amount = input()
print("Please enter any additional descriptions for this income: ")
description = input()
print("Thank you.\n")
with open(income_file_path,"a") as f_income:
f_income.write(f"\n{name}\t\t{amount}\t\t{description}")