Python: Search a string from multiple Text files
What I want to do:
- Extract sample1.tgz file.
- Store into 'sample1' directory
- Search a string from sample1/nvram2/log/TextFiles
Complete path => C:Usersusernamescriptssample1nvram2logsversion.txt
Note: TextFiles are with different extensions
Example:
textFile.txt
textFile.txt.0
textFile.txt.1
textFile.log
textFile
What I have tried:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
print("Reading " + current_file)
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#---Following code is to find the string from all the files in a directory---
path=output_file_path + 'nvram2logs*'
files=glob.glob(path)
for file1 in files:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
Issue:
I think, the problem is with path. It works, when I try to execute the code with the following code.
path='nvram2logs*.txt'
But it checks only for '.txt' file extensions. But I want to search for all file extensions.
It does not work when I try the following code. Here output_file_path
contains sample1
i.e. the directory name
path=output_file_path + 'nvram2logs*'
python python-3.x
add a comment |
What I want to do:
- Extract sample1.tgz file.
- Store into 'sample1' directory
- Search a string from sample1/nvram2/log/TextFiles
Complete path => C:Usersusernamescriptssample1nvram2logsversion.txt
Note: TextFiles are with different extensions
Example:
textFile.txt
textFile.txt.0
textFile.txt.1
textFile.log
textFile
What I have tried:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
print("Reading " + current_file)
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#---Following code is to find the string from all the files in a directory---
path=output_file_path + 'nvram2logs*'
files=glob.glob(path)
for file1 in files:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
Issue:
I think, the problem is with path. It works, when I try to execute the code with the following code.
path='nvram2logs*.txt'
But it checks only for '.txt' file extensions. But I want to search for all file extensions.
It does not work when I try the following code. Here output_file_path
contains sample1
i.e. the directory name
path=output_file_path + 'nvram2logs*'
python python-3.x
add a comment |
What I want to do:
- Extract sample1.tgz file.
- Store into 'sample1' directory
- Search a string from sample1/nvram2/log/TextFiles
Complete path => C:Usersusernamescriptssample1nvram2logsversion.txt
Note: TextFiles are with different extensions
Example:
textFile.txt
textFile.txt.0
textFile.txt.1
textFile.log
textFile
What I have tried:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
print("Reading " + current_file)
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#---Following code is to find the string from all the files in a directory---
path=output_file_path + 'nvram2logs*'
files=glob.glob(path)
for file1 in files:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
Issue:
I think, the problem is with path. It works, when I try to execute the code with the following code.
path='nvram2logs*.txt'
But it checks only for '.txt' file extensions. But I want to search for all file extensions.
It does not work when I try the following code. Here output_file_path
contains sample1
i.e. the directory name
path=output_file_path + 'nvram2logs*'
python python-3.x
What I want to do:
- Extract sample1.tgz file.
- Store into 'sample1' directory
- Search a string from sample1/nvram2/log/TextFiles
Complete path => C:Usersusernamescriptssample1nvram2logsversion.txt
Note: TextFiles are with different extensions
Example:
textFile.txt
textFile.txt.0
textFile.txt.1
textFile.log
textFile
What I have tried:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
print("Reading " + current_file)
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#---Following code is to find the string from all the files in a directory---
path=output_file_path + 'nvram2logs*'
files=glob.glob(path)
for file1 in files:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
Issue:
I think, the problem is with path. It works, when I try to execute the code with the following code.
path='nvram2logs*.txt'
But it checks only for '.txt' file extensions. But I want to search for all file extensions.
It does not work when I try the following code. Here output_file_path
contains sample1
i.e. the directory name
path=output_file_path + 'nvram2logs*'
python python-3.x
python python-3.x
edited Nov 14 '18 at 10:09
Dipankar Nalui
asked Nov 13 '18 at 11:31
Dipankar NaluiDipankar Nalui
2291417
2291417
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
After extracting the files into folder, you can use os.walk to visit all files in the given path and do your comparison.
Example Code:
import os
# Extract tar file
# ...
# ...
path = output_file_path + r'nvramlogs'
for dirpath, dirs, files in os.walk(path):
# dirpath : current dir path
# dirs : directories found in currect dir path
# files : files found in currect dir path
# iterate each files
for file in files:
# build actual path of the file by joining to dirpath
file_path = os.path.join(dirpath, file)
# open file
with open(file_path) as file_desc:
# iterate over each line, enumerate is used to get line count
for ln_no, line in enumerate(file_desc):
if string_to_search in line:
print('Filename: {}'.format(file))
print('Text: {}'.format(line.strip()))
print('Line No: {}n'.format(ln_no + 1))
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
|
show 1 more comment
Here is the full code that solved the issue:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#----Following code is to find the string from all the files in a directory
path1=output_file_path + r'nvram2logs'
all_files=glob.glob(os.path.join(path1,"*"))
for my_file1 in glob.glob(os.path.join(path1,"*")):
if os.path.isfile(my_file1): # to discard folders
with open(my_file1, errors='ignore') as my_file2:
for line_no, line in enumerate(my_file2):
if string_to_search in line:
print(string_to_search + " is found in " + my_file1 + "; Line Number = " + str(line_no))
Got help from this answer. The path and file not found issue was resolved by "Joining the directory with the filename solves it."
add a comment |
You could add a condition to check whether '.txt' is present in the file1
files= os.listdir(output_file_path + '/nvram2/logs/')
for file1 in files:
if '.txt' in file1:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
|
show 9 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280106%2fpython-search-a-string-from-multiple-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
After extracting the files into folder, you can use os.walk to visit all files in the given path and do your comparison.
Example Code:
import os
# Extract tar file
# ...
# ...
path = output_file_path + r'nvramlogs'
for dirpath, dirs, files in os.walk(path):
# dirpath : current dir path
# dirs : directories found in currect dir path
# files : files found in currect dir path
# iterate each files
for file in files:
# build actual path of the file by joining to dirpath
file_path = os.path.join(dirpath, file)
# open file
with open(file_path) as file_desc:
# iterate over each line, enumerate is used to get line count
for ln_no, line in enumerate(file_desc):
if string_to_search in line:
print('Filename: {}'.format(file))
print('Text: {}'.format(line.strip()))
print('Line No: {}n'.format(ln_no + 1))
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
|
show 1 more comment
After extracting the files into folder, you can use os.walk to visit all files in the given path and do your comparison.
Example Code:
import os
# Extract tar file
# ...
# ...
path = output_file_path + r'nvramlogs'
for dirpath, dirs, files in os.walk(path):
# dirpath : current dir path
# dirs : directories found in currect dir path
# files : files found in currect dir path
# iterate each files
for file in files:
# build actual path of the file by joining to dirpath
file_path = os.path.join(dirpath, file)
# open file
with open(file_path) as file_desc:
# iterate over each line, enumerate is used to get line count
for ln_no, line in enumerate(file_desc):
if string_to_search in line:
print('Filename: {}'.format(file))
print('Text: {}'.format(line.strip()))
print('Line No: {}n'.format(ln_no + 1))
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
|
show 1 more comment
After extracting the files into folder, you can use os.walk to visit all files in the given path and do your comparison.
Example Code:
import os
# Extract tar file
# ...
# ...
path = output_file_path + r'nvramlogs'
for dirpath, dirs, files in os.walk(path):
# dirpath : current dir path
# dirs : directories found in currect dir path
# files : files found in currect dir path
# iterate each files
for file in files:
# build actual path of the file by joining to dirpath
file_path = os.path.join(dirpath, file)
# open file
with open(file_path) as file_desc:
# iterate over each line, enumerate is used to get line count
for ln_no, line in enumerate(file_desc):
if string_to_search in line:
print('Filename: {}'.format(file))
print('Text: {}'.format(line.strip()))
print('Line No: {}n'.format(ln_no + 1))
After extracting the files into folder, you can use os.walk to visit all files in the given path and do your comparison.
Example Code:
import os
# Extract tar file
# ...
# ...
path = output_file_path + r'nvramlogs'
for dirpath, dirs, files in os.walk(path):
# dirpath : current dir path
# dirs : directories found in currect dir path
# files : files found in currect dir path
# iterate each files
for file in files:
# build actual path of the file by joining to dirpath
file_path = os.path.join(dirpath, file)
# open file
with open(file_path) as file_desc:
# iterate over each line, enumerate is used to get line count
for ln_no, line in enumerate(file_desc):
if string_to_search in line:
print('Filename: {}'.format(file))
print('Text: {}'.format(line.strip()))
print('Line No: {}n'.format(ln_no + 1))
answered Nov 13 '18 at 12:12
Thejesh PRThejesh PR
339310
339310
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
|
show 1 more comment
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
No, This code does not give me the output.
– Dipankar Nalui
Nov 13 '18 at 12:30
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
It does not go inside the for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:35
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
outside os.walk loop, path variable prints "sample1/nvram/logs"
– Dipankar Nalui
Nov 13 '18 at 12:37
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
I added print statement to check. result shows, we are not able to go inside for loop of os.walk
– Dipankar Nalui
Nov 13 '18 at 12:40
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
Seems, the problem is to create the path
– Dipankar Nalui
Nov 13 '18 at 13:23
|
show 1 more comment
Here is the full code that solved the issue:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#----Following code is to find the string from all the files in a directory
path1=output_file_path + r'nvram2logs'
all_files=glob.glob(os.path.join(path1,"*"))
for my_file1 in glob.glob(os.path.join(path1,"*")):
if os.path.isfile(my_file1): # to discard folders
with open(my_file1, errors='ignore') as my_file2:
for line_no, line in enumerate(my_file2):
if string_to_search in line:
print(string_to_search + " is found in " + my_file1 + "; Line Number = " + str(line_no))
Got help from this answer. The path and file not found issue was resolved by "Joining the directory with the filename solves it."
add a comment |
Here is the full code that solved the issue:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#----Following code is to find the string from all the files in a directory
path1=output_file_path + r'nvram2logs'
all_files=glob.glob(os.path.join(path1,"*"))
for my_file1 in glob.glob(os.path.join(path1,"*")):
if os.path.isfile(my_file1): # to discard folders
with open(my_file1, errors='ignore') as my_file2:
for line_no, line in enumerate(my_file2):
if string_to_search in line:
print(string_to_search + " is found in " + my_file1 + "; Line Number = " + str(line_no))
Got help from this answer. The path and file not found issue was resolved by "Joining the directory with the filename solves it."
add a comment |
Here is the full code that solved the issue:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#----Following code is to find the string from all the files in a directory
path1=output_file_path + r'nvram2logs'
all_files=glob.glob(os.path.join(path1,"*"))
for my_file1 in glob.glob(os.path.join(path1,"*")):
if os.path.isfile(my_file1): # to discard folders
with open(my_file1, errors='ignore') as my_file2:
for line_no, line in enumerate(my_file2):
if string_to_search in line:
print(string_to_search + " is found in " + my_file1 + "; Line Number = " + str(line_no))
Got help from this answer. The path and file not found issue was resolved by "Joining the directory with the filename solves it."
Here is the full code that solved the issue:
import os,tarfile, glob
string_to_search=input("Enter the string you want to search : ")
#all_files holds all the files in current directory
all_files = [f for f in os.listdir('.') if os.path.isfile(f)]
for current_file in all_files:
if (current_file.endswith(".tgz")) or (current_file.endswith("tar.gz")):
tar = tarfile.open(current_file, "r:gz")
#file_name contains only name by removing the extension
file_name=os.path.splitext(current_file)[0]
os.makedirs(file_name) #make directory with the file name
output_file_path=file_name #Path to store the files after extraction
tar.extractall(output_file_path) #extract the current file
tar.close()
#----Following code is to find the string from all the files in a directory
path1=output_file_path + r'nvram2logs'
all_files=glob.glob(os.path.join(path1,"*"))
for my_file1 in glob.glob(os.path.join(path1,"*")):
if os.path.isfile(my_file1): # to discard folders
with open(my_file1, errors='ignore') as my_file2:
for line_no, line in enumerate(my_file2):
if string_to_search in line:
print(string_to_search + " is found in " + my_file1 + "; Line Number = " + str(line_no))
Got help from this answer. The path and file not found issue was resolved by "Joining the directory with the filename solves it."
answered Nov 14 '18 at 10:52
Dipankar NaluiDipankar Nalui
2291417
2291417
add a comment |
add a comment |
You could add a condition to check whether '.txt' is present in the file1
files= os.listdir(output_file_path + '/nvram2/logs/')
for file1 in files:
if '.txt' in file1:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
|
show 9 more comments
You could add a condition to check whether '.txt' is present in the file1
files= os.listdir(output_file_path + '/nvram2/logs/')
for file1 in files:
if '.txt' in file1:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
|
show 9 more comments
You could add a condition to check whether '.txt' is present in the file1
files= os.listdir(output_file_path + '/nvram2/logs/')
for file1 in files:
if '.txt' in file1:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
You could add a condition to check whether '.txt' is present in the file1
files= os.listdir(output_file_path + '/nvram2/logs/')
for file1 in files:
if '.txt' in file1:
with open(file1) as f2:
for line in f2:
if string_to_search in line:
#print file name which contains the string
print(file1)
#print the line which contains the string
print(str(line))
edited Nov 13 '18 at 13:32
answered Nov 13 '18 at 12:10
AI_LearningAI_Learning
2,6551729
2,6551729
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
|
show 9 more comments
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
No, it does not print anything inside for loop. It seems, it is not going inside for loop.
– Dipankar Nalui
Nov 13 '18 at 12:49
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I meant as you can fill the actions inside the 'for' loop. Corrected the action part. Kindly check
– AI_Learning
Nov 13 '18 at 12:51
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
I did the same. But it is not going inside the for loop.
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
for file1 in files: print("Hi")
– Dipankar Nalui
Nov 13 '18 at 13:06
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
I tried using a print() inside the for loop. It does not print anything inside for loop.
– Dipankar Nalui
Nov 13 '18 at 13:07
|
show 9 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280106%2fpython-search-a-string-from-multiple-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown