Can someone help please!

I need to search for a different set of strings like: 'fummy' or 'users' or 'logon' from a directory like this:

C:\mycase\Nic..\

A Text document is stored in there LIKE:
544, Text Document, 45, 588 KB

Can some help to write a script or python program to seach for this text strings

Thanks

Recommended Answers

All 25 Replies

Easy:

if 'fummy' in text:
    print 'fummy found in text'

Easy:

if 'fummy' in text:
    print 'fummy found in text'

Thank you for this... Please can you write fthe full script program for me. How to start the script.... How to specify the text document.... where searching will begin

Kindly help

import re, os

from os.path import join as pjoin, isdir
while True:
    targetfolder = raw_input('enter a target folder:\n').strip() #this is where the text file is located
    os.chdir(targetfolder)
    #here you need a line that opens the text file from the directory
    break # not sure if this is neccessary


searchword = raw_input('enter a search term:\n')

if searchword in text:    print "searchword was found in text"

Hope this helps, i wrote this outside of python because i dont have it at work so forgive me for the spacings ect, but i believe this is correct after a little play about, and hope it helps.

Search youtube if you need help with the last line, a lesson learned is infinately more valuable than information given.

if my script is wrong id appreciate the correction.

import re, os

from os.path import join as pjoin,

isdirwhile True:

targetfolder = raw_input('c:\python26\nick\784.txt:\n').strip()

os.chdir(targetfolder)

#here you need a line that opens the text file from the directory

break
searchword = raw_input('fummy:\n')

if searchword in text:

print "searchword was found in text"

ERROR

I input this programm on Programmer Notepad in order to execute the script on Python directory...

I got an error stating that...

File "Fummy.py", line 2
import re, os
identationError: unexpected indent.

Please what is this import re, os.... why did i need to put this.

I got the text document in C:\python26\nick\784.txt and I need to search for strings like 'fummy' or 'user' or 'logon' or 'date'

can someone help to debug this error and please a complete program so that I can begin to develop on this for more other searches. I am novice in programming please...

hang on, im working on it, forget the one i posted earlier

This works, but always returns "is not in text" even if it is :(

import re, os

from os.path import join as pjoin, isdir
while True:
    targetfolder = raw_input('enter a target folder:\n').strip()
    os.chdir(targetfolder)
    break

targetfile = raw_input('enter a file name:\n')
infile = open(targetfile)
searchword = raw_input('enter a search term:\n')
infile.readlines()
if searchword in (targetfile):
    print ("search word was found in text")

else:
    print ("search word is not in text")

import re, os
from os.path import join as pjoin, isdir
while True:
targetfolder = raw_input('c:\python26\nick:\n').strip()
os.chdir(targetfolder)
break
targetfile = raw_input('c:\python26\nick\784.txt:\n')
infile = open(targetfile)
searchword = raw_input('fummy:\n')
infile.readlines()
if searchword in (targetfile):
print ("search word was found in text")
*************************************
I have Input the above and save as fummy.py...
I executed it on c:\python26>python fummy.py
Error:File "fummy.py", line 1
import re, os

IndentationError: unexpected indent.....

CAN SOMEONE HELP TO DEBUG THIS PLEASE

you need to lay out the script exactly as i have, python uses indents instead of brackets to group statements, so where i have indents, you need them.

And i wouldnt recomend filling in the brackets where i have, the raw input is where you enter the file paths upon execution

you need to lay out the script exactly as i have, python uses indents instead of brackets to group statements, so where i have indents, you need them.

And i wouldnt recomend filling in the brackets where i have, the raw input is where you enter the file paths upon execution

**************************************************
Did the program script correct as shown above? is that the way to input it at the programmer notepad? what else will I do to correct the error?

import re, os
from os.path import join as pjoin, isdir
while True:
targetfolder = raw_input('c:\python26\nick:\784:n').strip()
os.chdir(targetfolder)
break
targetfile = raw_input('c:\python26\nick\784:\n')
infile = open(targetfile)
searchword = raw_input('fummy:\n')
infile.readlines()
if searchword in (targetfile):
print ("searchword was found in text")

else:
print )"searchword is not in text")
*************************************
I have Input the above and save as fummy784.py...
I executed it on c:\python26>python fummy784.py
Error:File "fummy784.py", line 3
from os.path import join as pjoin, isdir

IndentationError: unexpected indent.....

PLEASE SEE THE PROGRAM SCRIPT ABOVE AND CAN SOMEONE HELP TO DEBUG THIS PLEASE...

import re, os
from os.path import join as pjoin, isdir
while True:
targetfolder = raw_input('c:\python26\nick:\784:n').strip()
os.chdir(targetfolder)
break
targetfile = raw_input('c:\python26\nick\784:\n')
infile = open(targetfile)
searchword = raw_input('fummy:\n')
infile.readlines()
if searchword in (targetfile):
print ("searchword was found in text")

else:
print )"searchword is not in text")
*************************************
I have Input the above and save as fummy784.py...
I executed it on c:\python26>python fummy784.py
Error:File "fummy784.py", line 3
from os.path import join as pjoin, isdir

IndentationError: unexpected indent.....

PLEASE SEE THE PROGRAM SCRIPT ABOVE AND YOU CAN SEE ONE LAST ERROR TO DEBUG

When posting python code, please use python code tags

You have to post your code with code tags so we can see the indents.

And as this is your second thread on the same topic and you didn't answer my question in the first...is searching a single file what the assignment was supposed to do?

The assignment is to search for different set of strings like 'fummy' or 'user' 'logon' from a dump file converted to a string.

What about posting the code in code tags so we can see the indents and have a chance at being able to help?

import re, os

from os.path import join as pjoin,
while True:
targetfolder = raw_input('c:\mycase\nick\784:\n'). strip()
os.chdir(targetfolder)
if isdir (targetfolder):
break
else:
targetfile = raw_input('c:\python26\nick\784:\n')
infile = open(targetfile)
searchword = raw_input('fummy:\n')
infile.readlines()
if searchword in (targetfile):
print ("searchword was found in text")

else:
print ("searchword is not in text")
THIS IS THE PROGRAM....
I have different process id's, I need to search for various type of string text. I have the folder saved on python26 directory...

e.g. c:\python26\nick\784

I need to be able to run a script to examine the above dir with a tring text like 'fummy' user and generate an output file...
Please do help, I am novice in programming just learning it...

snippsat,
what is code tag? I do not know what you mean. I am novice in programming. sorry about this but please help

>>> import re, os
...   from os.path import join as pjoin, 
...   while True:
... targetfolder = raw_input('c:\mycase\nick\784:\n'). strip()
...   os.chdir(targetfolder)
...   if isdir (targetfolder):
...        break
...   else: 
...  targetfile = raw_input('c:\python26\nick\784:\n')
...   infile = open(targetfile)
...  searchword = raw_input('fummy:\n')
...   infile.readlines()
...   if searchword in (targetfile):
...   print ("searchword was found in text")

...      else:
...   print ("searchword is not in text")

I studied python identation on the net, I think I am right?

I took your code and touched it up a bit...

import os

# put this part back if you're really attached to changing directories
#while True:
    #targetfolder = raw_input('Enter target folder to search:\n')
    #if isdir (targetfolder):
        #os.chdir(targetfolder)
        #break
    
targetfile = raw_input('Enter file to search:\n')
infile = open(targetfile)

searchword = raw_input('Enter string to search for::\n')

found = False
for line in infile:
    if searchword in line:
        print line
        found = True

if not found:
    print "Searchword %s not found in file %s" % (searchword, targetfile)

Many thanks.
But you see, I still have this error :

>python fummy784.py....
File "fummy784.py", line 1
import os
indentationError : unexpected indent
**********
I think this is the only error that remain...

If you haven't gotten the hint yet...indentation (leading white-space) is CRITICAL to python programs.

In the case of the import os line...it should be all the way to the left...there should be no white-space characters in front of it.

Just like it was in the code I posted.

If you think you already have it to the left, paste ALL of your code here...(inside code tags)

[code=python] # Your code here

[/code]

It has been sorted out ... no indentation error again. I did it just as you have said it. But it is very slow... it is still looking for 'fummy' for over 10mins. I wondered if you could help. I do really appreciate you.

Hi Murtan,

I am still having problem with the execution time, it is over 30 mins with no result. I tried to reduce the size of the .txt file to 1mb to see if the program will be quicker. No result yet. Please help me out!

Error while executing this command:

import re, os
...from os.path import join as pjoin, isdir
...targetdir = raw_input('c:\volatility-1.3_Beta>c:\python26\)
...os.chdir(targetdir)
...if targetdir = raw_input
...then 
exec ('python.exe volatility pslist -f c:\python26\nick.img')

Kindly help!

if you're using code tags, you can leave the spaces where they are in the code. (don't add the periods)

The intent of code tags is to provide syntax highlighting, line numbers and to preserve white-space.

The 'code' you have posted (when I replace the '.'s with ' 's) won't even start to run...it has an indentation error (you seem to like them).


If you want help with your original progam's speed, why don't you post what you're trying to run (in code tags) and we can look at it.

If you have a new question on a new topic, you should start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.