problem when reading file

S

shrimpy

hi every one, i am new to python,
and coz i want to write a handy command for my linux machine, to find
a word in all the files which are under the current folder.

the code is half done, but when i run it, it complain, and i don`t know why???

can anyone help me have a look at it?


here is the message it complain
=======================================
file is : qt_plugins_3.3rc

Traceback (most recent call last):
File "./python_script/svn_grep.py", line 34, in <module>
searPatten(file,'are','no')
File "./python_script/svn_grep.py", line 8, in searPatten
openFile = open(file, 'r')
IOError: [Errno 2] No such file or directory: 'qt_plugins_3.3rc'


here are the code i wrote
---------------------------------------------------------------code begin
#!/usr/bin/python

import os
import string


def searPatten(file, patten, isCaseSensitive):
openFile = open(file, 'r')
lines = openFile.readlines()
lineNum = 1
for text in lines :
words = text.split()
existed = 0
for word in words :
if isCaseSensitive == 'yes' :
if word.lower() == patten.lower():
existed = 1
break
else:
if word == patten :
existed = 1
break
if existed == 1:
print "line : " + str(lineNum) + " " + text
lineNum += 1
openFile.close()



for root, dirs, files in os.walk(os.getcwd()):
for file in files:
print "======================================="
print "file is : " + file + "\n"
searPatten(file,'are','no')

---------------------------------------------------------------code end
 
B

Bruce Frederiksen

hi every one, i am new to python,
and coz i want to write a handy command for my linux machine, to find
a word in all the files which are under the current folder.

the code is half done, but when i run it, it complain, and i don`t know why???

can anyone help me have a look at it?


here is the message it complain
=======================================
file is : qt_plugins_3.3rc

Traceback (most recent call last):
File "./python_script/svn_grep.py", line 34, in <module>
searPatten(file,'are','no')
File "./python_script/svn_grep.py", line 8, in searPatten
openFile = open(file, 'r')
IOError: [Errno 2] No such file or directory: 'qt_plugins_3.3rc'


here are the code i wrote
---------------------------------------------------------------code begin
<snip>

for root, dirs, files in os.walk(os.getcwd()):
for file in files:
print "======================================="
print "file is : " + file + "\n"
searPatten(file,'are','no')

---------------------------------------------------------------code end

Try changing that last line to:

searPatten(os.path.join(root, file), 'are', 'no')


You'll have to import os.path too...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top