I want to search a string from multiple files
What I tried:
import os
path= 'sample1/nvram2/logs'
all_files=os.listdir(path)
for my_file1 in all_files:
print(my_file1)
with open(my_file1, 'r') as my_file2:
print(my_file2)
for line in my_file2:
if 'string' in line:
print(my_file2)
Output:
C:\Users\user1\scripts>python search_string_3.py
abcd.txt
Traceback (most recent call last):
File "search_string_3.py", line 6, in <module>
with open(my_file1, 'r') as my_file2:
FileNotFoundError: [Errno 2] No such file or directory: 'abcd.txt'
But file abcd.txt is present in C:\Users\user1\scripts\sample1\nvram2\logs
Why the Error shows that No such file or directory?
Using glob:
The following error was displayed when I use all_files=glob.glob(path)
instead of all_files=os.listdir(path)
C:\Users\user1\scripts>python search_string_3.py
sample1/nvram2/logs
Traceback (most recent call last):
File "search_string_3.py", line 7, in <module>
with open(my_file1, 'r') as my_file2:
PermissionError: [Errno 13] Permission denied: 'sample1/nvram2/logs'