Reputation: 4318
I want to use python
to find a text
file which its name is given inside the folders of my computer and return the path of the file. I know how it should be done with bash
script but I am wondering how I can use python for it in a fast way?
Upvotes: 0
Views: 32
Reputation: 168616
Here's how I'd do it:
import os
import argparse
parse=argparse.ArgumentParser(description="Find some files in some dirs")
parse.add_argument('FILE_OR_DIR', nargs='+')
args = parse.parse_args()
target_dirs = [d for d in args.FILE_OR_DIR if os.path.isdir(d)] or ['.']
target_files = [f for f in args.FILE_OR_DIR if not os.path.isdir(f)]
for d in target_dirs:
for root, _, files in os.walk(d):
for file in files:
if file in target_files:
print os.path.join(root, file)
Upvotes: 1