Reputation: 481
This is the structure of my directory. I want to go into the subdirectories that start with "F3E3" and then return the csv's that contain "0.1Hz" in the filename. There are two csv's in each subdirectory that contain "0.1Hz" in the filename. I only want to return the last created file.
DATA
F3E319
F3E319_TEST.csv
F3E319_TEST_0.1Hz_11_21.csv
F3E319_TEST_0.1Hz_11_20.csv
F3E319_TEST_0.1Hz.png
F3E320
F3E320_TEST.csv
F3E320_TESTTEST_0.1Hz_11_21.csv
F3E320_TESTTEST_0.1Hz_11_20.csv
F3E320_TEST_0.1Hz.png
F3E321
F3E320_TEST.csv
F3E320_TEST2_0.1Hz_11_21.csv
F3E320_TEST2_0.1Hz_11_20.csv
F3E320_TEST_0.1Hz.png
F3ES1
F3ES1_TEST.csv
F3ES1_TEST15_0.1Hz.csv
Desired Output:
DATA\F3E319\F3E319_TEST_0.1Hz_11_21.csv
DATA\F3E320\F3E320_TESTTEST_0.1Hz_11_21.csv
DATA\F3E321\F3E320_TEST2_0.1Hz_11_21.csv
I have been using this code to find all the csv's with "0.1Hz":
import os.path
keyword = "0.1Hz"
for dirpath, dirnames, filenames in os.walk(r"\DATA"):
for filename in [f for f in filenames if f.endswith(".csv")]:
if keyword in filename:
print(os.path.join(dirpath, filename))
I cannot figure out how to only go into the sub-directories that start with "F3E3" and how to only return the last created csv with "0.1Hz".
I know that you can do something like this, but I'm not sure how to incorporate it into my current code:
for file in glob.glob('\DATA\F3E3*):
print file
Upvotes: 0
Views: 86
Reputation: 2318
You should use os.listdir
to iterate through the subdirectories and csv files so that is easier to find the last created file in the subdirectories. Since getting file creation time may varies depending on the operating system you're using (see How to get file creation & modification date/times in Python?), I add the creation_date
function and use it to sort the list of csv files.
import os
import platform
def creation_date(path_to_file):
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_birthtime
except AttributeError:
return stat.st_mtime
directory = "DATA"
subdirs = [os.path.join(directory, fl) for fl in os.listdir(directory) if fl.startswith("F3E3")]
for subdir in subdirs:
filenames = [os.path.join(subdir, fl) for fl in os.listdir(subdir) if fl.endswith(".csv") and ("0.1Hz" in fl)]
filenames=sorted(filenames, key=creation_date, reverse=True)
print(filenames[0])
Upvotes: 1