I have created a simple program to collect some data from the internet by downloading a zip file and accessing its contents using Python.
The program downloads the zip file and extracts it using ZipFile.extractall(). Then I use Python open() method to read the extracted files.
I need the data to be collected daily, so I have pasted a shortcut of the original program in the startup directory, so that everytime I start my laptop, the program starts the execution.
On running the program using python IDLE, the program runs perfectly, without errors, as shown:
But when the program is being executed automatically on startup, the program is run as an .exe file, and gives me the permission denied error:
Using print() statements I have cornered the code which is producing the error:
zip_file = 'C:/Users/Tirth/Downloads/'+'cm'+day+month_name+year+'bhav.csv.zip'
with ZipFile(zip_file, 'r') as zf:
zf.extractall()
file_name = 'C:/Users/Tirth/Documents/Programming/pyprojects/'+'cm'+day+month_name+year+'bhav.csv'
csv = open(file_name)
The problem seems to happen at zf.extractall(), but I guess permission has been denied for accessing the extracted files.
And you can also see that I am trying to open the extracted files in a different directory (C:/Users/Tirth/Documents/Programming/pyprojects) than what is shown in the error (C:/Windows/system32)
So this is the dilemma. Why is permission denied for python.exe files but not when executed using Python IDLE, and how can I fix this issue?
extractall()
extracts files to the current working directory, which is not necessarily the directory the archive file you are extracting from is located.
You can determine the current working directory with
import os
print(os.getcwd())
how can I fix this issue?
Pass the path to extract to as an argument in extractall():
zf.extractall(path='C:/Users/Tirth/Documents/Programming/pyprojects')