|
| 1 | +# Author: OMKAR PATHAK |
| 2 | + |
| 3 | +# This program will help us implement concepts such as binary searching, operating system. |
| 4 | +# P.S: Dont run this on root. That is dont give the DIRECTORY path as root else the program might |
| 5 | +# consume all your resources and your system might get crashed |
| 6 | + |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +DIRECTORY = '/home/omkarpathak/Desktop' |
| 11 | + |
| 12 | +# List all the directories in the DIRECTORY |
| 13 | +dirs = [name for name in os.listdir(DIRECTORY) if os.path.isdir(os.path.join(DIRECTORY, name))] |
| 14 | + |
| 15 | +# List all the files in the DIRECTORY |
| 16 | +# files = [name for name in os.listdir(DIRECTORY) if os.path.isfile(os.path.join(DIRECTORY, name))] |
| 17 | +files = [] |
| 18 | + |
| 19 | +for root, dirs, files in os.walk(DIRECTORY): |
| 20 | + for File in files: |
| 21 | + files.append(root + File) |
| 22 | + |
| 23 | +dirs.sort() |
| 24 | +files.sort() |
| 25 | + |
| 26 | +def binarySearch(target, List): |
| 27 | + '''This function performs a binary search on a sorted list and returns the position if successful else returns -1''' |
| 28 | + left = 0 #First position of the list |
| 29 | + right = len(List) - 1 #Last position of the list |
| 30 | + global iterations |
| 31 | + iterations = 0 |
| 32 | + |
| 33 | + while left <= right: #U can also write while True condition |
| 34 | + iterations += 1 |
| 35 | + mid = (left + right) // 2 |
| 36 | + if target == List[mid]: |
| 37 | + return mid, List[mid] |
| 38 | + elif target < List[mid]: |
| 39 | + right = mid - 1 |
| 40 | + else: |
| 41 | + left = mid + 1 |
| 42 | + return -1 |
| 43 | + |
| 44 | +print(dirs) |
| 45 | +print(files) |
| 46 | + |
| 47 | +try: |
| 48 | + result, filePath = binarySearch('server.py', files) |
| 49 | + print(os.path.abspath(filePath)) |
| 50 | +except: |
| 51 | + print('File not found') |
0 commit comments