Re: Regex Python Help
On Wed, 25 Mar 2015 14:19:39 -0700, Gregg Dotoli wrote: > On Wednesday, March 25, 2015 at 4:36:01 PM UTC-4, Denis McMahon wrote: >> On Tue, 24 Mar 2015 11:13:41 -0700, gdotoli wrote: >> >> > I am creating a tool to search a filesystem for one simple string. >> >> man grep >> >> STOP! REINVENTING! THE! WHEEL! >> >> Your new wheel will invariably be slower and less efficient than the >> old one. > Grep is regular expressions. If I'm using Python, I'll use the Python > modules. > Silly 1. Please don't top post, this is usenet, we don't top post, comments go after the text they comment on soi we can read down the page and it makes sense. 2. You gave the thread the title of "regex python help". 3. Your initial comment was "I am creating a tool to search a filesystem for one simple string." 4. The tool (see 3) already exists, it's called grep, it uses regular expressions (see 2). It's also going to be a lot faster than using python. 5. According to your post, grep seems to be the tool you are looking for. 6. Reinventing grep in python seems much more silly to me, by the time you've finished writing and testing the python code (especially if you need to seek help from a newsgroup in the process) grep would have found and identified every file containing your "one simple string". -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On 3/25/2015 7:10 PM, Steven D'Aprano wrote: Does Windows shell have grep? How about Powershell? No. I just use Idle's grep (Find in Files). And I generally would even if Command Prompt did have grep. Idle's has the nice feature that output goes in an Output Window, with each line found prefaced with filename and line number. One can then right click on a line and open the file at that line in an Idle editor. One can also save the list to a file if so inclined. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On 25/03/2015 23:10, Steven D'Aprano wrote: On Thu, 26 Mar 2015 08:19 am, Gregg Dotoli wrote: Grep is regular expressions. If I'm using Python, I'll use the Python modules. Silly It very well may be silly. You're using Python regular expressions because you're using Python. Why are you using Python? Early in this thread you said "Here you go. Windows shell was easier!!!" Does Windows shell have grep? How about Powershell? If grep is available, why mix two different languages? gnuwin32 certainly does. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Thu, 26 Mar 2015 08:19 am, Gregg Dotoli wrote: > Grep is regular expressions. If I'm using Python, I'll use the Python > modules. Silly It very well may be silly. You're using Python regular expressions because you're using Python. Why are you using Python? Early in this thread you said "Here you go. Windows shell was easier!!!" Does Windows shell have grep? How about Powershell? If grep is available, why mix two different languages? -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On 25/03/2015 21:19, Gregg Dotoli wrote: Grep is regular expressions. If I'm using Python, I'll use the Python modules. Silly Gregg Clear as mud, and please don't top post. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Grep is regular expressions. If I'm using Python, I'll use the Python modules. Silly Gregg On Wednesday, March 25, 2015 at 4:36:01 PM UTC-4, Denis McMahon wrote: > On Tue, 24 Mar 2015 11:13:41 -0700, gdotoli wrote: > > > I am creating a tool to search a filesystem for one simple string. > > man grep > > STOP! REINVENTING! THE! WHEEL! > > Your new wheel will invariably be slower and less efficient than the old > one. > > -- > Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Tue, 24 Mar 2015 11:13:41 -0700, gdotoli wrote: > I am creating a tool to search a filesystem for one simple string. man grep STOP! REINVENTING! THE! WHEEL! Your new wheel will invariably be slower and less efficient than the old one. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
No worries Steven, Thanks to ALL on this thread. Gregg On Tuesday, March 24, 2015 at 9:43:58 PM UTC-4, Steven D'Aprano wrote: > On Wed, 25 Mar 2015 05:13 am, gdot...@gmail.com wrote: > > > The error is: > > > > SyntaxError: Missing parentheses in call to 'print' > > > I cannot imagine how the message could be more explicit: the call to print > is missing parentheses. If you're not going to read the error messages you > are given, you are truly going to struggle as a programmer. > > Read the error message. If you don't understand the error message, please > say so. > > And choose a relevant subject line: this has nothing to do with regexes. You > might as well have called it "Import Python Help". > > > -- > Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Wed, 25 Mar 2015 05:13 am, gdot...@gmail.com wrote: > The error is: > > SyntaxError: Missing parentheses in call to 'print' I cannot imagine how the message could be more explicit: the call to print is missing parentheses. If you're not going to read the error messages you are given, you are truly going to struggle as a programmer. Read the error message. If you don't understand the error message, please say so. And choose a relevant subject line: this has nothing to do with regexes. You might as well have called it "Import Python Help". -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Here you go. Windows shell was easier!!! for /f %a in (c:\gonow) do echo %a | c:\Python34\python c:\python34\unopy.py %a Now I can use any regex pattern, I need. On Tuesday, March 24, 2015 at 3:54:25 PM UTC-4, Rob Gaddi wrote: > On Tue, 24 Mar 2015 12:43:38 -0700, Gregg Dotoli wrote: > > > [context snipped due to top posting] > > > > All I need is a loop, should I bag Python and use a simple shell for > loop? > > Honestly, yes. You're not even using a regular expression, just a fixed > string you're trying to search for. You can do the entire thing as > > $ find . -type f | xargs grep DECRYPT_I > > -- > Rob Gaddi, Highland Technology -- www.highlandtechnology.com > Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On 3/24/2015 2:13 PM, gdot...@gmail.com wrote: I am creating a tool to search a filesystem for one simple string. I cannot get the syntax correct. Thank you in advance for your help. import sys import re import os path='/' viewfiles=os.listdir(path) listdir is not recursive, so this code will only search files in the one directory, not the whole filesystem. You need to use os.walk and modify the code to do the latter. for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, You appear to have used a mixture of spaces and tabs for indents. That works in 2.x, but not in 3.x. You open but do not close files, which could be a problem if you open and search 10 files in a filesystem. Use a with statememt. 'allfiles' is a bad name because it get bound to a single file. for file in viewfiles: with open(os.path.join(path, file)) as text: for line in text: if re.match("DECRYPT_I", line): print(line) -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Tue, 24 Mar 2015 12:43:38 -0700, Gregg Dotoli wrote: > [context snipped due to top posting] > > All I need is a loop, should I bag Python and use a simple shell for loop? Honestly, yes. You're not even using a regular expression, just a fixed string you're trying to search for. You can do the entire thing as $ find . -type f | xargs grep DECRYPT_I -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Le 24/03/2015 20:22, Gregg Dotoli a écrit : Thank you! But The print error is gone, but now the script quickly finishes and doesnt walk the OS tree or search. Gregg On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote: I am creating a tool to search a filesystem for one simple string. I cannot get the syntax correct. Thank you in advance for your help. import sys import re import os path='/' viewfiles=os.listdir(path) for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, -- This should search every file for the simple regex "DECRYPT_I" This is from the root down. The error is: SyntaxError: Missing parentheses in call to 'print' Please help. Gregg Dotoli Your indentation is wrong. for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, Vincent -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
This works fine , but I have to pipe the filename to the script python stool I am creating a tool to search a filesystem for one simple string. > I cannot get the syntax correct. > Thank you in advance for your help. > > import sys > import re > import os > path='/' > viewfiles=os.listdir(path) > for allfiles in viewfiles: > file= os.path.join(path, allfiles) > text=open(file, "r") > for line in text: > if re.match("DECRYPT_I", line): > print line, > > -- > This should search every file for the simple regex "DECRYPT_I" > This is from the root down. > > The error is: > > SyntaxError: Missing parentheses in call to 'print' > > Please help. > Gregg Dotoli -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Tue, 24 Mar 2015 12:10:24 -0700, Gregg Dotoli wrote: > > Thank you Gary, that got rid of the error, but now there is no tree > walk, it runs and immediatley finishes. I just need to grep each file. I > have this working with the windows "for /r %a and redirecting that to > Python, but want to use Python only. I do have dummy files with the > regex string. > > Thanks again, > Gregg > Gregg -- First, please don't top-post. Secondly, os.listdir only does exactly that; it lists everything on one directory. You're looking for os.walk. -- Rob -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Tue, Mar 24, 2015 at 2:22 PM, Gregg Dotoli wrote: > The print error is gone, but now the script quickly finishes and doesnt > walk > the OS tree or search. > You need to walk the directory tree recursively. Take a look at os.walk(). Skip -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Le 24/03/2015 20:38, Vincent Vande Vyvre a écrit : Le 24/03/2015 20:22, Gregg Dotoli a écrit : Thank you! But The print error is gone, but now the script quickly finishes and doesnt walk the OS tree or search. Gregg On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote: I am creating a tool to search a filesystem for one simple string. I cannot get the syntax correct. Thank you in advance for your help. import sys import re import os path='/' viewfiles=os.listdir(path) for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, -- This should search every file for the simple regex "DECRYPT_I" This is from the root down. The error is: SyntaxError: Missing parentheses in call to 'print' Please help. Gregg Dotoli Your indentation is wrong. for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, Vincent ... and me too for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Thank you! But The print error is gone, but now the script quickly finishes and doesnt walk the OS tree or search. Gregg On Tuesday, March 24, 2015 at 2:14:32 PM UTC-4, Gregg Dotoli wrote: > I am creating a tool to search a filesystem for one simple string. > I cannot get the syntax correct. > Thank you in advance for your help. > > import sys > import re > import os > path='/' > viewfiles=os.listdir(path) > for allfiles in viewfiles: > file= os.path.join(path, allfiles) > text=open(file, "r") > for line in text: > if re.match("DECRYPT_I", line): > print line, > > -- > This should search every file for the simple regex "DECRYPT_I" > This is from the root down. > > The error is: > > SyntaxError: Missing parentheses in call to 'print' > > Please help. > Gregg Dotoli -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
Thank you Gary, that got rid of the error, but now there is no tree walk, it runs and immediatley finishes. I just need to grep each file. I have this working with the windows "for /r %a and redirecting that to Python, but want to use Python only. I do have dummy files with the regex string. Thanks again, Gregg On Tuesday, March 24, 2015 at 2:34:20 PM UTC-4, Gary Herron wrote: > On 03/24/2015 11:13 AM, gdot...@gmail.com wrote: > > I am creating a tool to search a filesystem for one simple string. > > I cannot get the syntax correct. > > Thank you in advance for your help. > > > > import sys > > import re > > import os > > path='/' > > viewfiles=os.listdir(path) > > for allfiles in viewfiles: > > file= os.path.join(path, allfiles) > > text=open(file, "r") > > for line in text: > > if re.match("DECRYPT_I", line): > > print line, > > > > -- > > This should search every file for the simple regex "DECRYPT_I" > > This is from the root down. > > > > The error is: > > > > SyntaxError: Missing parentheses in call to 'print' > > > > Please help. > > Gregg Dotoli > > You appear to be using Python3, but have written code for Python2. > > There are a number of differences between the two, but your particular > error runs into the different syntax for prints. > > Python2: print line # This is a statement > Python3 print(line) # This is a procedure call > > > > -- > Dr. Gary Herron > Department of Computer Science > DigiPen Institute of Technology > (425) 895-4418 -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On 03/24/2015 11:13 AM, gdot...@gmail.com wrote: I am creating a tool to search a filesystem for one simple string. I cannot get the syntax correct. Thank you in advance for your help. import sys import re import os path='/' viewfiles=os.listdir(path) for allfiles in viewfiles: file= os.path.join(path, allfiles) text=open(file, "r") for line in text: if re.match("DECRYPT_I", line): print line, -- This should search every file for the simple regex "DECRYPT_I" This is from the root down. The error is: SyntaxError: Missing parentheses in call to 'print' Please help. Gregg Dotoli You appear to be using Python3, but have written code for Python2. There are a number of differences between the two, but your particular error runs into the different syntax for prints. Python2: print line # This is a statement Python3 print(line) # This is a procedure call -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -- https://mail.python.org/mailman/listinfo/python-list
Re: Regex Python Help
On Tue, Mar 24, 2015 at 1:13 PM, wrote: > SyntaxError: Missing parentheses in call to 'print' It appears you are attempting to use a Python 2.x print statement with Python 3.x Try changing the last line to print(line.rstrip()) Skip -- https://mail.python.org/mailman/listinfo/python-list