How to efficiently check for a proper sequence of files?
I need to check a directory for the presence of a sequence of files.
For example at the root directory, the requirement is that if there's a file 开发者_如何学Pythonnamed: love.dat
, then there should be 3 files related to it such that there are:
love_p.dat
love_r.dat
love_q.dat
Of course, the directory contains other files as well and directories too. But no file related to another is located in different directories. So if love.dat
is at the root directory all its related files are in the root directory too.
I'm using java btw and all other information available to me is the list of base file names with relations (e.g. in the above scenario, love
is stored in the list of base files as the string love
)
Pseudocode:
def check_directory(directory):
"""Returns whether the directory has all the required names."""
names = ["love.dat", "love_p.dat", "love_r.dat", "love_q.dat"]
return all(os.path.exists(os.path.join(directory, x) for x in names))
In particular, note this uses "smarter" filename manipulation by using a path joining method (which is OS-specific), but it is extremely likely normal string manipulation would work fine for that here; plus note it tests for the existence of these names — how to do that depends on your exact environment and language — while the rest is just boilerplate to set that up.
If "love" is a stem instead of literal name, you need to add it dynamically to the list of names:
def check_directory(directory, stem):
"""Returns whether the directory has all the required names."""
names = [stem + x for x in [".dat", "_p.dat", "_r.dat", "_q.dat"]]
return all(os.path.exists(os.path.join(directory, x) for x in names))
print check_directory("/", "love") # example use
And if you want to check for all possible stems in a given directory, you simply need to loop over the names in that directory:
def find_file_groups(directory):
"""Returns groups of files as tuples of (base, _p, _r, _q)."""
for name in os.listdir(directory):
if name.endswith(".dat"): # apply more filters if required
base = name[:-4] # remove .dat
names = tuple(base + x for x in [".dat", "_p.dat", "_r.dat", "_q.dat"])
if all(os.path.exists(os.path.join(directory, x) for x in names)):
yield names
精彩评论