![]() |
Fixing "PermissionError: [Errno 13] Permission denied" - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Fixing "PermissionError: [Errno 13] Permission denied" (/thread-24268.html) Pages:
1
2
|
Fixing "PermissionError: [Errno 13] Permission denied" - puredata - Feb-06-2020 Hello, I'm trying to use Python to automate unzipping of multiple files in a folder. I already have a script that works and unzips my files. But it won't copy contents of the zip files which has subfolders. In order to be able to extract everything into one main folder (disregarding original subfolder structure) I have found this code chunk on stackexchange. A moderator reported that the code is working for him therefore I should be investigating something else, not the code itself. import os import shutil import zipfile my_dir = r"C:\Users\username\My_Dataset\new" my_zip = r"C:\Users\username\My_Dataset" with zipfile.ZipFile(my_zip) as zip_file: for member in zip_file.namelist(): filename = os.path.basename(member) # skip directories if not filename: continue # copy file (taken from zipfile's extract) source = zip_file.open(member) target = open(os.path.join(my_dir, filename), "wb") with source, target: shutil.copyfileobj(source, target)Here is the Traceback I get: The only thing that looks suspicious to me is the direction of the slashes on Traceback. Some of them are forwards slashes while some of them are backwards. Could it be related to that? I'm running this code in Windows and haven't tried on a mac or linux.Would it be possible to get some suggestions in order to make this script work? I don't need to use this specific script, I'm just trying to unzip a lot of files (some are under subfolders some are not) into one main folder. Thanks in advance! RE: Fixing "PermissionError: [Errno 13] Permission denied" - stullis - Feb-06-2020 According to the docs, zipfile.ZipFile() expects a file, not a directory. I'd wager that's the culprit. Try creating the file first and passing that into zipfile.ZipFile(). RE: Fixing "PermissionError: [Errno 13] Permission denied" - jim2007 - Feb-06-2020 Is there a folder called C:\Users\username on your machine? That looks very strange in the error message you posted. I would expect it to be C:\Users\<your login Id>\My_Dataset RE: Fixing "PermissionError: [Errno 13] Permission denied" - puredata - Feb-07-2020 (Feb-06-2020, 11:41 PM)stullis Wrote: According to the docs, zipfile.ZipFile() expects a file, not a directory. I'd wager that's the culprit. Try creating the file first and passing that into zipfile.ZipFile(). Thanks for looking it up! I checked the documentation as well but I couldn't comprehend to the point to solve my issue. What do you mean by creating the file first? Do you mean creating a shapefile or do you mean that I need to change the order in the script? (Feb-06-2020, 11:52 PM)jim2007 Wrote: Is there a folder called C:\Users\username on your machine? That looks very strange in the error message you posted. Thank you, I replaced username before posting since it's my real name in there with the actual path. Do you think backwards/forwards slashes might be creating the issue here? RE: Fixing "PermissionError: [Errno 13] Permission denied" - puredata - Feb-07-2020 Wanted to up this post, I still couldn't figure out a way to unzip these files. RE: Fixing "PermissionError: [Errno 13] Permission denied" - jim2007 - Feb-07-2020 So two things to check: - Check if you can create a file in the folder with notepad for instance - Your variable my_zip points to a folder name and not a file. It should be something like: my_zip = r"C:\Users\username\My_Dataset\<name>.zip" Let us know how you go. RE: Fixing "PermissionError: [Errno 13] Permission denied" - puredata - Feb-08-2020 (Feb-07-2020, 10:50 PM)jim2007 Wrote: So two things to check: Thank you, I can create a file, actually I have another script, I run from PyCharm and it works on the folder, but this script gives me a permission error. The reason my_zip points to a folder is I have a folder with zip files and I'm trying to go through them in order to extract their content into a main file. The script I have (which works) does the job but it only extract one layer of zip files, if a zip file has a subfolder in it (not necessarily a zipped folder) it won't extract those to my "main" folder. This is what I'm trying to achieve with the script that gives me an error. Thank you for your help again. RE: Fixing "PermissionError: [Errno 13] Permission denied" - TheHacker707 - Feb-08-2020 If permission really is the problem, you could try to run the program with admin user level. Also, I would keep your slashes as backslashes if you're using Windows. -707 RE: Fixing "PermissionError: [Errno 13] Permission denied" - puredata - Feb-08-2020 (Feb-08-2020, 03:41 AM)TheHacker707 Wrote: If permission really is the problem, you could try to run the program with admin user level. Also, I would keep your slashes as backslashes if you're using Windows. Now I'm trying the same on a mac and don't get the permission error but I get this instead so I imagine stullis seems correct that this script expects a single zip file. (Feb-06-2020, 11:41 PM)stullis Wrote: According to the docs, zipfile.ZipFile() expects a file, not a directory. I'd wager that's the culprit. Try creating the file first and passing that into zipfile.ZipFile(). Moving on to my previous script; import os import zipfile user: str = os.getlogin() #to be used in directory names #item = 'item' # i am not sure if this is necessary dir_name = "/Users/{0}//Documents/Boundary_Datasets".format(user) #folder that contains multiple zipfiles which some of them have subfolders in those zipfiles extract = "/Users/{0}//Documents/Boundary_Datasets/new".format(user) #main folder to be used as an output folder extension = ".zip" #file_name = dir_name + "/" + item #i am not sure if this is necessary os.chdir(dir_name) # change directory from working dir to dir with files for item in os.listdir(dir_name): # loop through items in dir if item.endswith(extension): # check for ".zip" extension file_name = os.path.abspath(item) # get full path of files zip_ref = zipfil e.ZipFile(file_name) # create zipfile object zip_ref.extractall(extract) # extract file to dir zip_ref.close() # close file # os.remove(file_name) # delete zipped fileCan anyone help me to modify this so that it not only extracts the first subfolder content but all subfolders and extract all files into one main folder? RE: Fixing "PermissionError: [Errno 13] Permission denied" - jim2007 - Feb-08-2020 It is almost certainly an issue with passing the correct file name to the function. Try hard coding one of the file names into the my_zip variable and see if it runs without error. |