-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterate_path.py
executable file
·60 lines (49 loc) · 1.48 KB
/
iterate_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# Imports
from pathlib import Path
import argparse
# Parameters
args_parser = argparse.ArgumentParser(
prog="show-path", description="Lists all folders and files in a directory"
)
args_parser.add_argument(
"directory",
metavar="search/this/directory",
type=str,
help="Enter directory to be searched.",
)
args_parser.add_argument(
"-p",
"--print",
action="store_true",
help="Print found list of directories and files.",
required=False,
)
# Create list objects
directories = []
files = []
# Functions
def iterate_directory(search_path):
"""Iterates through directory and adds found files or directories to their respective lists"""
all_path_objects = search_path.glob("*")
for path_object in all_path_objects:
if path_object.is_dir():
directories.append(path_object)
else:
files.append(path_object)
# Main
if __name__ == "__main__":
args = args_parser.parse_args()
iterate_directory(Path(args.directory))
print(f"We have found {len(directories)} directories and {len(files)} files.")
if args.print:
if len(files) != 0:
print("Here is a list of files found.")
print(*files, sep="\n")
else:
print("No files found to print.")
if len(directories) != 0:
print("Here is a list of directories found.")
print(*directories, sep="\n")
else:
print("No directories found to print.")