0

I'm trying to write a simple script that will return a count of files of a specific type in a folder. So far, I can get it to work for whatever folder I run it from, but I'm trying to find a way to alter it so I can drop it in a parent and return an individual count for each folder it looks at.

from pathlib import Path
import os
p = Path(Path.cwd())
p.glob('*')
files = list(p.glob('*.txt'))
number_files = len(files)
print(number_files)
exit = (input('Press any key to exit'))

This is what I've got so far. I know it works where it is because I'm pulling the current working directory, but I wanted to leave it flexible enough that I can drop it into wherever I need it and just change the filetype it's counting as I need to.

CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • 2
    Could you please rephrase your question? I'm not sure exactly what you're trying to achieve – Omer Tuchfeld Mar 09 '20 at 15:56
  • To clarify, are you trying to count the number of files of a certain type in a directory? And you'd like to do this recursively? – manny Mar 09 '20 at 15:56
  • 1
    I think you mean "folder" and not "file". I edited your question accordingly. It matches the code now. If you think this is wrong, feel free to change it again. – CherryDT Mar 09 '20 at 15:57
  • Also, you probably want to use a command line argument. See https://stackoverflow.com/a/4033743/1871033 - you can use `sys.argv[1]` instead of `Path.pwd()` to specify the folder, for example. – CherryDT Mar 09 '20 at 15:58
  • @CherryDT Folders are files on a Linux/Unix based system. – Rashid 'Lee' Ibrahim Mar 09 '20 at 15:58
  • 2
    @Rashid true, but this phrasing makes it much more clear. – manny Mar 09 '20 at 15:59
  • Technically yes, a folder (or maybe I should have used "directory") is just a file with a directory flag set. But this is not the way you'd talk about it... I don't think anyone would understand "count of files in a file" the right way! – CherryDT Mar 09 '20 at 15:59
  • Hm but @jdinges93 maybe you can still clarify a bit, because I realize now that the title doesn't make sense either. Your question says that you want to have it flexible and specify a different directory or filetype but your title says something about "going to the next file" – CherryDT Mar 09 '20 at 16:00
  • FWIW, the concept of the "next file" of a directory makes no sense. – martineau Mar 09 '20 at 16:02
  • I think the actual question is how can he loop through subdirectories of any given directory that he runs the script in. Which as been answered https://stackoverflow.com/questions/19587118/iterating-through-directories-with-python – Rashid 'Lee' Ibrahim Mar 09 '20 at 16:04
  • Sorry for the lack of clarity in the question. I'm working on a windows computer, and I'm trying to make it return the counts of the specified file type (right now it's .txt, but it could be anything). I want to be able to place it in a folder, and have it return the count of those files from each of the subfolders in the parent. (maybe it would be better phrased as parent directory/subdirectories?) – jdinges93 Mar 09 '20 at 21:07
  • @Rashid'Lee'Ibrahim I think that's close to what I was looking for, but is there a way to do it without defining the "root directory" specifically? Could I use the Path.cwd() to define that instead of writing the actual directory? – jdinges93 Mar 10 '20 at 13:32
  • @jdinges93 Instead of giving the direct path, put os.getcwd() – Rashid 'Lee' Ibrahim Mar 10 '20 at 16:06

0 Answers0