Python: fnmatch – Compare filenames by patterns.

[code language="python"]
"""
"fnmatch" module compares file names against glob-style patterns such as used by Unix shells.
Find more information:
https://pymotw.com/2/fnmatch/
http://www.pythonforbeginners.com/fnmatch/os-walk-and-fnmatch-in-python
"""

import os
from fnmatch import fnmatch

root = '//Users//computerLoginName//Documents//'
extension = 'test*.py'

counts = 0

for path, dirs, files in os.walk(root):
for name in files:
if fnmatch(name, extension):
print(os.path, name)

# count repeated files inside directory
#num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
#print(num_files)

# count number of files
counts += 1
print(counts)
[/code]

Comments

Popular posts from this blog

Robot Framework vs Cucumber

Performance Testing of RESTful APIs Using JMeter

Verification displayed number of rows inside table by Robot Framework