First you create rootFolder twice, which is not the problem.
The last line just print all elements, because they are not an instance of ZipFile nor RarFile.
They are all strings.
This makes your comprehension a little bit long. You can use a function to decide if an element is added or using multiline.
Multiline example:
In this case, you can convert the Path object with
The benefit of globbing is, that you only get the matching files.
The '**/*.rar' means, that also subdirectories are included.
https://docs.python.org/3/library/pathli....Path.glob
The last line just print all elements, because they are not an instance of ZipFile nor RarFile.
They are all strings.
zipfiles = [os.path.join(rootFolder, f) for f in os.listdir(rootFolder) if f.endswith('.rar') or f.endswith('.zip')]This should give you a list with strings, where only strings are inside which ends with .rar or .zip.
This makes your comprehension a little bit long. You can use a function to decide if an element is added or using multiline.
Multiline example:
zipfiles = [ os.path.join(rootFolder, f) for f in os.listdir(rootFolder) if f.endswith('.rar') or f.endswith('.zip') ]Or with a decider function:
def is_archive(file): register = ('.rar', '.zip') return any(file.endswith(ftype) for ftype in register) zipfiles = [os.path.join(rootFolder, f) for f in os.listdir(rootFolder) if is_archive(f)]Another approach can be the use of pathlib in combimation with glob.
from pathlib import Path archive_folder = Path('your path') rar_archives = list(archive_folder.glob('**/*.rar') zip_archives = list(archive_folder.glob('**/*.zip')In the lists *_archive are Paths stored. There are some functions/modules, which can't handle Path objects.
In this case, you can convert the Path object with
str(your_path_element)
to a str.The benefit of globbing is, that you only get the matching files.
The '**/*.rar' means, that also subdirectories are included.
https://docs.python.org/3/library/pathli....Path.glob
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!