Is there a way to scan a directory to see how many files of certain type there is?

为君一笑 提交于 2020-04-18 03:57:29

问题


So if I have a directory like below is there a way to scan how many files with a specific code in its name. For example if I want the number of files that start with 17042020 which would be 6 from the directory below?

1704202001-01.csv
1704202001-02.csv
1704202002-01.csv
1704202002-02.csv
1704202003-01.csv
1704202003-02.csv
001.png
002.pdf
003.docx
004.jpg

回答1:


Use os to get a list with your files :

import os
list = os.listdir("path")
for element in list:
     if yourconditions:
          print(element)



回答2:


you can use the pathlib module or just plain glob :

 from pathlib import Path
 folder = Path(dir_of_files)
 specific_files = list(folder.glob('1704202001-*.csv')
 #or : list(folder.rglob('1704202001-*.csv') ... this will recursively search thru every subfolder for files that match this

 print(specific_files)



回答3:


On Unix/Linux/macOS (so with your OS), you can do it many ways in shell.

Assuming you are in the specified folder, you can do for example:

ls | grep "1704202001" | wc | awk '{print $1}'

ls will list your folder files/subfolders grep will filter your search with only lines containing your pattern wc will count numbers of lines/characters of the search awk will be told to print only the first column (wc will answer 3 numbers, only the first one is interesting for us)

If you want some recursive search, you can use find

find . -name "*1704202001*" | wc | awk '{print $1}'

find will perform a search in . and all the subfolders for the RegExp pattern (so we use the wildcard * to match the pattern in a complete file name).

Last but not list, you may want to count how many files are containing your pattern (not in the name, but inside the file himself). You can do it using grep:

grep -R "1704202001" | wc | awk '{print $1}' 

You were asking for Python but also operating-system help, this answer is for the last :)

Hope it will helps some of you.




回答4:


Use pathlib module which is now best for paths

import pathlib
from typing import List

"""!
@brief Finds files with the given unique code in name in directory
@param[in] directory pathlib.Path directory of searching
@param[in] unique_code str code in the filename
@return List[pathlib.Path] list of filepaths with unique code in name
"""
def find_files(directory:pathlib.Path, unique_code:str) -> List[pathlib.Path]:
    result:List[pathlib.path] = list()
    for filepath in directory.glob('*'):
        if filepath.is_file() and unique_code in filepath.name:
            result.append(filepath)
    return result

# Set Your directory!
directory:pathlib.Path = pathlib.Path('your/dir')

unique_code:str = '17042020'

found_files:List[pathlib.Path] = find_files(directory, unique_code)

print(f"Found files with unique code [{unique_code}]: {len(found_files)}")
for filepath in found_files:
        print(f"    {filepath.resolve()}")


来源:https://stackoverflow.com/questions/61271956/is-there-a-way-to-scan-a-directory-to-see-how-many-files-of-certain-type-there-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!