-
Notifications
You must be signed in to change notification settings - Fork 612
/
Copy pathP77_FileSearching.py
51 lines (40 loc) · 1.49 KB
/
P77_FileSearching.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
# Author: OMKAR PATHAK
# This program will help us implement concepts such as binary searching, operating system.
# P.S: Dont run this on root. That is dont give the DIRECTORY path as root else the program might
# consume all your resources and your system might get crashed
import os
from pathlib import Path
DIRECTORY = '/home/omkarpathak/Desktop'
# List all the directories in the DIRECTORY
dirs = [name for name in os.listdir(DIRECTORY) if os.path.isdir(os.path.join(DIRECTORY, name))]
# List all the files in the DIRECTORY
# files = [name for name in os.listdir(DIRECTORY) if os.path.isfile(os.path.join(DIRECTORY, name))]
files = []
for root, dirs, files in os.walk(DIRECTORY):
for File in files:
files.append(root + File)
dirs.sort()
files.sort()
def binarySearch(target, List):
'''This function performs a binary search on a sorted list and returns the position if successful else returns -1'''
left = 0 #First position of the list
right = len(List) - 1 #Last position of the list
global iterations
iterations = 0
while left <= right: #U can also write while True condition
iterations += 1
mid = (left + right) // 2
if target == List[mid]:
return mid, List[mid]
elif target < List[mid]:
right = mid - 1
else:
left = mid + 1
return -1
print(dirs)
print(files)
try:
result, filePath = binarySearch('server.py', files)
print(os.path.abspath(filePath))
except:
print('File not found')