-
Notifications
You must be signed in to change notification settings - Fork 586
/
P04_FindIfAFileExists.py
30 lines (25 loc) · 1.12 KB
/
P04_FindIfAFileExists.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
# Author: OMKAR PATHAK
# This script illustrates how to find if a file exists on the system with the specified name
import os
# Path IN which we have to search file
PATH = '/home/omkarpathak/Documents/GITs/Python-Programs/Scripts' # Give your path here
def searchFile(fileName):
''' This function searches for the specified file name in the given PATH '''
for root, dirs, files in os.walk(PATH):
print('Looking in:',root)
for Files in files:
try:
found = Files.find(fileName) # Returns -1 if NOT found else returns index
# print(found)
if found != -1:
print(fileName, 'Found')
break
except:
exit()
if __name__ == '__main__':
searchFile('6-Folder.txt')
# OUTPUT:
# omkarpathak@omkarpathak-Inspiron-3542:~/Documents/GITs/Python-Programs/Scripts$ python P04_FindIfAFileExists.py
# Looking in: /home/omkarpathak/Documents/GITs/Python-Programs/Scripts
# Looking in: /home/omkarpathak/Documents/GITs/Python-Programs/Scripts/Tests
# 6-Folder.txt Found