-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesearch.py
123 lines (97 loc) · 3.56 KB
/
filesearch.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Write a script that searches a folder you specify (as well as its subfolders, up
# to two levels deep) and compiles a list of all `.jpg` files contained in there.
# The list should include the complete path of each `.jpg` file.
#
# You should train:
#
# - Using `for` loops
# - Using conditional statements
# - Working with `pathlib`
# - Thinking about nested loops
#
# If you are feeling bold, create a list containing each type of file extension
# you find in the folder.
# Start with a small folder to make it easy to check whether your program is
# working correctly. Then search a bigger folder.
# This program should work for any specified folder on your computer.
from pathlib import Path
import os, os.path
# establish the default path which is the current directory
default_path = Path.cwd()
current_path = default_path
chosen_files = []
# choose a path if you know the directory exactly
def choose_path():
global default_path, current_path
# understand if the user knows the exact path
answer = input('\nDo you want to choose a path? [Y/N]: ')
# keep asking for the correct input until it is received
while True:
if answer == 'N':
print('\nThe current directory path is used as a default.')
print("default selected")
path = Path.cwd()
break
elif answer == 'Y':
while True:
home_dir = Path.cwd().home()
path = default_path.home().joinpath(input(f'\nEnter the path after the home directory {home_dir}/'))
print(path)
if path.exists():
break
else:
print('\nThe path doesn''t exist. Try again.')
break
current_path = path
print(current_path)
return current_path
# lists all folders in the given path (optional) or current directory (default)
def choose_folder():
global current_path
print(current_path)
# create a list and have a dummy entry as the 0th variable
folders = ['empty']
print('\nThese are all your folders in the directory: \n')
# iterate through all the files in the directory
for item in current_path.iterdir():
# print the files that are folders (directories)
if item.is_dir():
# add the folder path into the list and print the index and the names of the folders
folders.append(item)
print(folders.index(item), item.name)
#print(folders)
while True:
folder_number = int(input('\nChoose a folder number: '))
if folder_number >= len(folders):
continue
break
current_path = folders[folder_number]
return current_path
def question():
global default_path
global current_path
choose_path()
current_path = choose_folder()
while True:
answer = input('\nWould you like to open another folder in the current directory? [Y/N] ')
if answer=='N':
print(f'You are currently in: {current_path}')
break
elif answer=='Y':
current_path = choose_folder()
continue
else:
print('Type the correct answer.')
continue
return current_path
def search(path):
global chosen_files
for root, dirs, files in os.walk(path):
print(root)
for file in files:
if os.path.splitext(file)[1]=='.py':
chosen_files.append(os.path.join(root, file))
print(file)
return chosen_files
search(Path('c:/Users/kenan/Desktop/codingnomads'))
print(chosen_files)