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"
#16
Here a more simple version, not tested much:

from pathlib import Path
from typing import Generator


FileList = Generator[Path, None, None]


def get_list_of_files(base_folder: Path) -> FileList:
    for entry in base_folder.rglob('*'):
        # Skip all hidden files and folders
        if entry.name.startswith('.'):
            continue
            
        if entry.is_file():
            yield entry


base_folder = Path.home() / 'Development'
files = list(get_list_of_files(base_folder))

for file in files:
    print(file)
Btw. the typehint stuff is not needed, but they could help an IDE to check for errors.
I changed the FileList, because it's now a generator, which yields Path objects.
You can make the homepath with Path.home() and join paths with /.
Makes it more readable. Also the recursion is not needed and can fail, if your directory tree is deeper than 1000.
The method rglob('*') does the job and find all files and directories recursive.

Now to you other problem. You can extend the function, to take a argument for extensions you want to process.

from pathlib import Path

my_extensions = ['.txt', '.odt', '.csv']
my_path = Path('/A/directory/somewhere/that/does/not.exist/file.txt')
print('The suffix is:', my_path.suffix)

if my_path.suffix not in my_extensions:
    print(my_path, 'has not the allowed file extension')
Instead of print something, you continue in the for-loop, to skip this element.

By the way, you could iterate over the generator. Before you provided a list as argument, which was modified inside the function. Now the function is turned into a generator (the yield keyword is inside). For each iteration the generator returns a path.

If the file-list is not needed, you could remove files = []

 
for file in get_list_of_files(base_folder):
    print(file)
    my_zipfile = zipfile.ZipFile(file)
    my_zipfile.extractall(r'C:\Users\username\My_Dataset\new')
Maybe the zipfile Module doesn't understand the Path object (haven't tested yet), but a path could converted into a string with str(file).
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Circumvent the "access denied" page? Pedroski55 7 392 Jun-15-2024, 06:25 AM
Last Post: Pedroski55
  The INSERT permission was denied on the object Steven5055 3 1,751 Jun-12-2024, 08:13 AM
Last Post: GregoryConley
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,466 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  KivyMD android app - problem with permission polak7gt 0 395 Jan-18-2024, 01:27 PM
Last Post: polak7gt
  Potential Permission error on Mac OSX Catalina OWOLLC 1 904 Nov-02-2023, 07:52 AM
Last Post: unjnsacih
  logging: change log file permission with RotatingFileHandler erg 0 1,244 Aug-09-2023, 01:24 PM
Last Post: erg
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,776 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,954 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Server Folder Error : WinError5 Access Denied fioranosnake 1 1,228 Jun-21-2022, 11:11 PM
Last Post: Larz60+
  Permission issue when using scapy jao 3 10,664 Feb-05-2022, 06:14 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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