Python Forum
Fixing "PermissionError: [Errno 13] Permission denied"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fixing "PermissionError: [Errno 13] Permission denied"
#15
(Feb-10-2020, 04:51 PM)jim2007 Wrote: So I did the same, but it is a function that returns a list of files"

Can't appreciate your time enough Jim, this lists all the files in my folder and I'm able to somewhat understand it! (except the fact that you use base_folder in the function but define it later on, I thought these codes were interpreted line by line) but that's something that I can research myself.

Now I'll go ahead and try to export these files that we printed into a main folder. I'll resurrect the thread if I get stuck again. :)

Thanks a bunch!

Edit: it didn't take long for me to come up with another question. Here is my final script;

import os, zipfile, shutil
from typing import List

FileList = List[str]


def get_list_of_files(base_folder: str, files: FileList) -> None:
    with os.scandir(base_folder) as entries:
        for entry in entries:
            # Skip all hidden files and folders
            if entry.name.startswith('.'):
                continue

            qualified_name = os.path.join(base_folder, entry)

            # If it's a file we'll added it to the collection
            if entry.is_file():
                files.append(qualified_name)
            elif entry.is_dir():
                # If it's a folder then we'll process that as well
                get_list_of_files(os.path.join(base_folder, entry), files)


files = []
base_folder = r"C:\Users\username\My_Dataset"

get_list_of_files(base_folder, files)

for file in files:
    print(file)
    my_zipfile = zipfile.ZipFile(file)
    my_zipfile.extractall(r'C:\Users\username\My_Dataset\new')

# Generate the file paths to traverse, or a single path if a file name was given
def getfiles(path):
    if os.path.isdir(path):
        for root, dirs, files in os.walk(path):
            for name in files:
                yield os.path.join(root, name)
    else:
        yield path


destination = r"C:\Users\Documents\flatten"
fromdir = r"C:\Users\username\My_Dataset\new"
for f in getfiles(fromdir):
    filename = str.split(f, '/')[-1]
    if os.path.isfile(destination + filename):
        filename = f.replace(fromdir, "", 1).replace("/", "_")
    # os.rename(f, destination+filename)
    shutil.copy2(f, r"C:\Users\username\Documents\flatten")
Here is my question; what if I only want to process files that have specific extensions and leave the rest? I imagine I should build an if else function but I'm not sure exactly where to put it and how to construct it.

For example

if filetype is = shp, shx, cpg, prj, dbf
    continue 
Would you have any input about this? Thanks a lot again in advance!
Reply


Messages In This Thread
RE: Fixing "PermissionError: [Errno 13] Permission denied" - by puredata - Feb-10-2020, 07:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  KivyMD android app - problem with permission polak7gt 0 348 Jan-18-2024, 01:27 PM
Last Post: polak7gt
  Potential Permission error on Mac OSX Catalina OWOLLC 1 802 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
  logging: change log file permission with RotatingFileHandler erg 0 1,152 Aug-09-2023, 01:24 PM
Last Post: erg
  The INSERT permission was denied on the object Steven5055 2 1,570 Feb-25-2023, 11:37 PM
Last Post: Steven5055
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,697 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,877 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,181 Jun-21-2022, 11:11 PM
Last Post: Larz60+
  Permission issue when using scapy jao 3 10,273 Feb-05-2022, 06:14 PM
Last Post: snippsat
  Error no 13: Permission denied in python shantanu97 1 6,304 Mar-31-2021, 02:15 PM
Last Post: snippsat
  Invalid syntax error - need help fixing calgk01 3 3,399 Feb-23-2021, 08:41 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020