Python Forum
Counting the number of files related to particular format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting the number of files related to particular format
#1
In a folder there are different file formats such as jpeg,jpg,pdf and psd. Following program can count the files in a particular format.If the folder gets a new file format then a new function needs to be added.So like adding a new function for every file format how to write a more generalized program for any file types.
if the files are added dynamically


import os 
filter = []
for filepath,dir,filenames in os.walk(r'C:\Users\sai\Desktop\dosc'):
		for element in filenames:
			filter.append(element)



newstr = filter
print(len(newstr))


def jpegfilter(x):
	jpeglist =[]
	for line in x:
		if line.endswith('.jpeg') | line.endswith('.jpg') | line.endswith('.JPG'):
			jpeglist.append(line)

	return jpeglist

def pdffilter(x):
	pdflist = []
	for line in x:
		if line.endswith('.pdf'):
			pdflist.append(line)

	return pdflist

def psdfilter(x):
	psdlist = []
	for line in x:
		if line.endswith('.psd'):
			psdlist.append(line)

	return psdlist

def docfilter(x):
	doclist = []
	for line in x:
		if line.endswith('.doc'):
			doclist.append(line)

	return doclist


print("number of jpeg files-{}".format(len(jpegfilter(newstr))))
print("number of pdf files-{}".format(len(pdffilter(newstr))))
print("number of doc files-{}".format(len(docfilter(newstr))))
print("number of doc files-{}".format(len(psdfilter(newstr))))
print("Total number of files-{}".format( len(jpegfilter(newstr)) + len(pdffilter(newstr)) + len(docfilter(newstr)) + len(psdfilter(newstr))) )
Reply


Messages In This Thread
Counting the number of files related to particular format - by ambush - Nov-05-2018, 03:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read directory listing of files and parse out the highest number? cubangt 5 2,370 Sep-28-2022, 10:15 PM
Last Post: Larz60+
Photo How can I use 2 digits format for all the number? plumberpy 6 2,348 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,681 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Although this is a talib related Q it's mostly related to python module installing.. Evalias123 4 5,690 Jan-10-2021, 11:39 PM
Last Post: Evalias123
  Counting Number of Element in a smart way quest 2 2,013 Nov-09-2020, 10:24 PM
Last Post: quest
  multiple number format conversion oli_action 4 2,584 Aug-11-2020, 05:10 AM
Last Post: perfringo
  get method not counting number of strings in dictionary LearningTocode 2 2,114 Jun-13-2020, 11:17 PM
Last Post: LearningTocode
  counting items in a list of number combinations Dixon 2 2,085 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Number format william888 3 2,840 Aug-23-2019, 05:33 AM
Last Post: ThomasL
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,547 Aug-12-2019, 01:31 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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