Python 如何消除django xlrd中的权限错误
我尝试通过导入包含详细信息的excel来创建模型字段,但得到了权限错误。有什么问题吗 景色Python 如何消除django xlrd中的权限错误,python,django,xlrd,Python,Django,Xlrd,我尝试通过导入包含详细信息的excel来创建模型字段,但得到了权限错误。有什么问题吗 景色 def UploadTeacherView(request): message='' if request.method == 'POST': form = NewTeachersForm(request.POST, request.FILES) if form.is_valid(): excel_file = request.FIL
def UploadTeacherView(request):
message=''
if request.method == 'POST':
form = NewTeachersForm(request.POST, request.FILES)
if form.is_valid():
excel_file = request.FILES['file']
try:
import os
import tempfile
import xlrd
fd, tmp = tempfile.mkstemp() # create two temporary file
with os.open(fd, 'wb') as out: # create new file objects
out.write(excel_file.read())
book = xlrd.open_workbook(tmp)
print(book)
sheet = book.sheet_by_index(0)
obj=TeacherData(
code = sheet.cell_value(rowx=1, colx=1),
first_name = sheet.cell_value(rowx=2, colx=1),
last_name = sheet.cell_value(rowx=3, colx=1),
email = sheet.cell_value(rowx=4, colx=1),
phone = sheet.cell_value(rowx=5, colx=1),
)
obj.save()
finally:
os.remove(tmp)
else:
message='Invalid Entries'
else:
form = NewTeachersForm()
return render(request,'new_teacher.html', {'form':form,'message':message})
下面是我上传文件时遇到的错误
PermissionError at /teachers/upload
[WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\FRGULI~1\\AppData\\Local\\Temp\\tmpm87yli8l'
回溯(最近一次呼叫最后一次):
UploadTeacherView中的文件“D:\Python\Django\Links Online exames\Links\u Online\u Results\teachers\views.py”,第41行
将os.open(fd,'wb')作为out:#创建新的文件对象
TypeError:open:路径应为字符串、字节或os.PathLike,而不是int
在处理上述异常期间,发生了另一个异常:
Traceback (most recent call last):
File "D:\Python\Django\Links Online Exams\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\Python\Django\Links Online Exams\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python\Django\Links Online Exams\Links_Online_Results\teachers\views.py", line 55, in UploadTeacherView
os.unlink(tmp)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\FRGULI~1\\AppData\\Local\\Temp\\tmpkdyl14y2'
您的Excel文件已由另一个进程(Excel)打开。或者,目录中还有一个剩余的锁文件。我如何摆脱这个?????代码的格式是否正确@Laurent Laport关闭Excel应用程序并再次运行代码。任何地方都没有打开Excel…您让我们猜测错误发生在哪里。请更新问题以包含完整的错误消息回溯。