【问题标题】:Python chmod, Permission issuePython chmod,权限问题
【发布时间】:2021-01-21 03:28:00
【问题描述】:

我只是遇到了 chmod 方法的问题。我并不完全了解编程,但我有一些经验,我正在尝试编写一个脚本来跟踪我的小型家庭企业的收入和支出。

我认为问题在于写入文件的部分或 chmod 方法,两者都靠近底部:

weeklyDatetime = datetime.now()
weeklyDate = weeklyDatetime.strftime("%Y, %m, %V - Incomes")

incomeFileNameAndDirectory = incomesPath + "\\" + weeklyDate + ".txt"
weeklyIncomeFile= open(incomeFileNameAndDirectory, "w+")
weeklyIncomeFile.close()

makingChoice = True
while makingChoice == True:
    print("1. Add another Income\n"
          "2. Stop adding incomes")
    
    addIncomeOrNo = input()

    if addIncomeOrNo == "1":
       print("Please enter the name of this income: ")
        incomeName = input()
        print("Please enter the amount of this income: ")
        incomeAmount = input()
        print("Please enter any additional descriptions for this income: ")
        incomeDescription = input()
        print("Thank you.\n")

        with open(incomesPath, "a") as weeklyIncomeFile:
            os.chmod(incomeFileNameAndDirectory, stat.S_IRWXO)
            weeklyIncomeFile.write("\n" + incomeName + "\t\t" + incomeAmount + "\t\t" + incomeDescription)

我想我可能没有正确使用 os.chmod 方法?我仍然收到以下错误:

PermissionError: [Errno 13] Permission denied: 'C:\Users\iarko\Desktop\PCE Business\Financials\Incomes'

请告诉我问题是什么,感谢您的阅读!

【问题讨论】:

    标签: python-3.x chmod


    【解决方案1】:

    主要问题在于您的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}")
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-14
      • 2020-11-22
      • 1970-01-01
      • 2011-03-25
      • 2012-07-20
      • 2019-05-09
      • 2013-06-26
      • 2011-04-15
      相关资源
      最近更新 更多