Python 得到一个;[ERRNO13]拒绝许可”;读取文件时出错,即使我自己拥有777权限
我的代码:Python 得到一个;[ERRNO13]拒绝许可”;读取文件时出错,即使我自己拥有777权限,python,json,python-3.x,permissions,file-permissions,Python,Json,Python 3.x,Permissions,File Permissions,我的代码: # -*- coding: utf-8 -*- """ Created on Sun Jan 22 14:47:36 2017 @author: Jose Chong """ import json import tkinter import os def checkExistenceOfSaveFile(): if not os.path.isfile(filepath): os.makedirs(filepath, 777) checkExisten
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 14:47:36 2017
@author: Jose Chong
"""
import json
import tkinter
import os
def checkExistenceOfSaveFile():
if not os.path.isfile(filepath):
os.makedirs(filepath, 777)
checkExistenceOfSaveFile()
filepath = os.path.expanduser(r'~\Documents\joseDzirehChongToDoList\toDoListSaveFile.json')
master = tkinter.Tk()
master.title("To-Do List")
master.geometry("300x300")
masterFrame = tkinter.Frame(master)
masterFrame.pack(fill=tkinter.X)
checkboxArea = tkinter.Frame(masterFrame, height=26)
checkboxArea.pack(fill=tkinter.X)
inputStuff = tkinter.Frame(masterFrame)
checkboxList = []
if os.path.getsize(filepath) == 0:
with open (filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
with open(filepath) as infile:
checkboxList = json.load(infile)
def destroyCheckbox(checkbox, row):
row.destroy()
del checkboxList [checkboxList.index(str(checkbox))]
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
for savedCheckbox in checkboxList:
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text=savedCheckbox)
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text="x", bg="red", fg="white",
activebackground="white", activeforeground="red",
command=lambda c=savedCheckbox, r=checkboxRow: destroyCheckbox(c, r))
deleteItem.pack(side=tkinter.RIGHT)
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
def drawCheckbox():
newCheckboxInput = entry.get()
def destroyCheckbox():
checkboxRow.destroy()
del checkboxList[checkboxList.index(newCheckboxInput)]
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
checkboxList.append(newCheckboxInput)
entry.delete(0,tkinter.END)
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1])
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", command=lambda c=checkbox1, r=checkboxRow: destroyCheckbox(), bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
with open(filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
def createInputStuff():
paddingFrame = tkinter.Frame(inputStuff, height=5)
paddingFrame.pack(fill=tkinter.X)
buttonDone.pack()
inputStuff.pack()
buttonAdd.pack_forget()
master.bind('<Return>', lambda event: drawCheckbox())
def removeInputStuff():
inputStuff.pack_forget()
buttonAdd.pack()
buttonDone.pack_forget()
master.unbind('<Return>')
buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff)
buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff)
buttonAdd.pack()
topInput = tkinter.Frame(inputStuff)
bottomInput = tkinter.Frame(inputStuff)
topInput.pack()
bottomInput.pack()
prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?")
prompt.pack()
entry = tkinter.Entry(bottomInput, bd=3)
entry.pack(side=tkinter.LEFT)
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox)
buttonConfirm.pack(side=tkinter.LEFT)
master.mainloop()
此代码的上下文如下所示:
if os.path.getsize(filepath) == 0:
with open (filepath, 'w') as outfile:
json.dump(checkboxList, outfile)
它检查toDoListSaveFile.json是否为空(filepath
是文件的文件路径),并检查是否添加了空列表。之所以这样做,是因为我以后有了json.load()
,您无法加载空文件
如您所见,我使用777权限创建了该文件,如
checkExistenceOfSaveFile()
中所示。这不意味着每个人都可以读写文件吗?或者我必须更改权限,以便我的程序可以向其写入内容。在定义它所依赖的文件路径之前,请调用checkExistenceOfSaveFile()
。这绝对是一个错误。另外,checkExistenceOfSaveFile()
创建一个目录,而不是一个文件,并且您无法写入目录,这就是权限被拒绝的原因。在定义文件路径之前,您可以调用checkExistenceOfSaveFile()
,它依赖于该路径。这绝对是一个错误。另外,checkExistenceOfSaveFile()
会创建一个目录,而不是文件,并且您无法写入目录,这就是权限被拒绝的原因。
with open (filepath, 'w') as outfile:
if os.path.getsize(filepath) == 0:
with open (filepath, 'w') as outfile:
json.dump(checkboxList, outfile)