The system cannot find the file specified (django)
Hi I'm creating pdf file but I face an error [WinError 2] The system cannot find the file specified but I don't know where is the error
if os.path.exists(pdf_file_output):
with open(pdf_file_output, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/pdf")
response['Content-Disposition'] = 'attachment; filename=' + os.path.basename(pdf_file_output)
return response
Your code is unable to locate the file specified (probably in Windows). There are few reasons:
Wrong file path: Check if that pdf_file_output
file path is correct and the file is exist in that location.
Permission problem: Make sure that the current user has permissions to access the file.
Typo in the filename: Check if that there are no typos in the file name and the file name is the same as you're trying to access.
Alternative script that helps you to debug your code:
import os
pdf_file_output = ...
if os.path.exists(pdf_file_output):
with open(pdf_file_output, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/pdf")
response['Content-Disposition'] = 'attachment; filename=' +
os.path.basename(pdf_file_output)
return response
else:
print(f"The file '{pdf_file_output}' doesn't exist.")