Issue
I'm using Python3, Linux Mint and Visual Studio Code.
Also using, the following code to see whether any evtx files exist in a directory:
print("Number of EVTX files in each individual folder:")
path = '/home/user/CI5235_K1915147_Sam/evtx_logs'
folders = ([name for name in os.listdir(path)])
targets = []
for folder in folders:
contents = os.listdir(os.path.join(path,folder))
for i in contents:
if i.endswith('evtx'):
targets.append(i)
print(folder, len(contents))
print ("Total number of evtx files = " + str(len(targets)))
print()
It searches the directory and sub-directories of evtx_logs and lists every file within. How would I be able to modify it so that it ONLY counts evtx files as it is currently counting other .xml files that are also in the folder too.
Solution
Modified your existing code to use glob
as suggested above.
#!/usr/bin/env python3
import os
import glob
print("Number of EVTX files in each individual folder:")
path = '/home/user/CI5235_K1915147_Sam/evtx_logs'
folders = ([name for name in os.listdir(path)])
targets = []
for folder in folders:
contents = glob.glob(os.path.join(path,folder)+"/*evtx")
for i in contents:
targets.append(i)
print(folder, len(contents))
print ("Total number of evtx files = " + str(len(targets)))
print()
The problem with your own code is that you're printing the length of contents (which will always be all the files in the directory) rather than the number of matches from if i.endswith('evtx'):
.
Answered By - tink Answer Checked By - Senaida (WPSolving Volunteer)