Python Forum
how to check for file type in a folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to check for file type in a folder
#2
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.

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!
Reply


Messages In This Thread
RE: how to check for file type in a folder - by DeaD_EyE - Sep-15-2018, 01:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 646 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Reading a file name fron a folder on my desktop Fiona 4 1,008 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  please check this i wanna use a csv file as a graph xCj11 5 1,566 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  Function not executing each file in folder mathew_31 9 2,398 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  check if a file exist on the internet and get the size kucingkembar 6 1,869 Apr-16-2022, 05:09 PM
Last Post: kucingkembar
  Trying to determine attachment file type before saving off.. cubangt 1 2,231 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,237 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Code to check folder and sub folders for new file and alert fioranosnake 2 2,008 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,600 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  How to import file and function in another folder SriRajesh 1 3,267 Dec-18-2021, 08:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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