出现此错误:
File "C:/Users/Admin/PycharmProjects/project/Project.py", line 207, in record_attendance
with open("SOFT_6017.txt", "w") as module_1:
PermissionError: [Errno 13] Permission denied: 'SOFT_6017.txt'
我在网上搜索了所有内容,却找不到解决方案的答案。我问了一个夫,他们不知道该怎么办。所以他们建议我在这里写一个问题。
SOFT_6017的内容:
Mary Martin,10,0,0
Alan Wilson,0,9,1
Alan Lowe,5,6,0
我要在其中添加详细信息的地方:
with open("SOFT_6017.txt", "w") as module_1:
soft_6017_1 = module_1.readline().split(',')
soft_6017_2 = module_1.readline().split(',')
soft_6017_3 = module_1.readline().split(',')
name_1_1 = soft_6017_1[0]
attended_1_1 = int(soft_6017_1[1])
missed_1_1 = int(soft_6017_1[2])
excused_1_1 = int(soft_6017_1[3])
name_2_1 = soft_6017_2[0]
attended_2_1 = int(soft_6017_2[1])
missed_2_1 = int(soft_6017_2[2])
excused_2_1 = int(soft_6017_2[3])
name_3_1 = soft_6017_3[0]
attended_3_1 = int(soft_6017_3[1])
missed_3_1 = int(soft_6017_3[2])
excused_3_1 = int(soft_6017_3[3])
count_1 = len(open("SOFT_6017.txt").readlines())
data = f"{name_1_1}, {attended_1_1}, {missed_1_1}, {excused_1_1} \n " \
f"{name_2_1}, {attended_2_1}, {missed_2_1}, {excused_2_1} \n " \
f"{name_3_1}, {attended_3_1}, {missed_3_1}, {excused_3_1}"
module_1.seek(0)
module_1.write(data)
第207行-以open(“ SOFT_6017.txt”,“ w”)作为module_1:此行。
答案 0 :(得分:1)
您正在尝试读取以写模式打开的文件。 更改
with open("SOFT_6017.txt", "w") as module_1:
对此
with open("SOFT_6017.txt", "r") as module_1:
要向文件中写入数据,请删除以下行:
module_1.write(data)
并打开另一个with
块:
with open("SOFT_6017.txt", "w") as mod_wr:
mod_wr.write(data)
希望这会有所帮助:)
答案 1 :(得分:0)
这是因为您可以打开存档“ SOFT_6017.txt”,然后尝试将其关闭并重试。