Hello,

I am trying to create a function that will search a fixed line of text for a word or phrase. Once that is completed, I am to try and pass the line of text and the word as parameters. Can someone help lead me in the right direction. I have literally just started learning this language for week. Im using python 3.1. Im using the terminal window in mac for python. Below is what I have done so far, which is to see if I can search a file for a specific string and return the results. The error message is also shown below. What am I doing wrong?

>>> def search_linetest():

...     path= '/Users/jolie_tia/Documents'
...     dir= os.listdir(path)
...     for file in dir:
...             open('/Users/jolie_tia/Documents/sample.txt')
...     s= file.read()
...     x= text.find('I was scared to pieces.')
...     if x != -1:
...             print ('Line was found starting at character', x)
...     else:
...             print ('Line not found')
... 
>>> 
>>> linetest()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in linetest
IOError: [Errno 2] No such file or directory: 'Users/jolie_tia/Documents/sample.text'

An easy way to error check this would be to add a new line after line five to print the results of your directory search.

print dir
look through the results and see if there is a misspelling in the document title.
My most recent file search was defeated by the file being named in creation "example.txt", ad was a .txt file. So it was really named "example.txt.txt" which was hard to spot.

Line six is opening the file sample.txt as many times as there is files in dir without saving it for later use. I think that is not what you are after. The error simply says that such file as does not exist, but it is strange that it is missing the root /.

You may like to see this demo (modify to suit your needs):

import os


def search_linetest():
    path = 'C:/Users/dwzavitz/Desktop/2014Files/DaniPython' #'/Users/jolie_tia/Documents'
    dir = os.listdir(path)
    for file in dir:
        print( file )
        if '.exe' in file:
            pass
        else:
            #open('/Users/jolie_tia/Documents/sample.txt')
            f = open( file )
            s = f.read()
            #x= s.find('I was scared to pieces.')
            x= s.find( 'print' )
            if x != -1:
                print( 'Line was found starting at character', x )
            else:
                print('Line not found')
            input( "Press 'Enter' to continue ... " )


search_linetest()

David thank you so much for your help. I tried the improved code and I still got errors. This is what I put:

>>> import os
>>> 
>>> def search_linetest():
...     path=('/Users/jolie_tia/Documents')
...     dir=os.listdir(path)
...     for file in dir:
...             print(file)
...             if '.exe' in file:
...                     pass
...             else:
...                     f=open('Users/jolie_tia/Documents/sample.txt')
...                     s=f.read()
...                     x= s.find('I was scared to pieces.')
...                     if x != -1:
...                             print ('Line was found starting at character', x)
...                     else:
...                             print('Line not found')
...                     input("Press 'Enter' to continue...")

This is the error I received:

>>> search_linetest()
 dbst665 project chat.docx
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in search_linetest
IOError: [Errno 2] No such file or directory: 'Users/jolie_tia/Documents/sample.txt'

I just wanted to look at the specific file sample.txt. I understand what the code is saying and trying to do, but it's not working.

The error message is very clear,Python can not find "sample.txt" in that path.

with open('C:/Users/jolie_tia/Documents/sample.txt') as f:
    print f.name

'''Output-->
Message File Name   Line    Position
    Traceback
<module> <module4>    1
IOError: [Errno 2] No such file or directory:                               'C:/Users/jolie_tia/Documents/sample.txt'
'''

Here i fixing the error by making folders for that path and put "sample.txt" in Documents folder.

with open('C:/Users/jolie_tia/Documents/sample.txt') as f:
    print f.name

'''Output-->
C:/Users/jolie_tia/Documents/sample.txt
'''

Check your path to file again,cd to folder in Linux(terminal) an open sample.txt.
I guess you use Linux because you have not specified a drive letter.
If you use Windows,you do of course need to specifi a drive letter as i do.

Dont use file as a variable name,file is a reserved word used bye Python.

Dont use file as a variable name,file is a reserved word used bye Python.

Hey ... sorry I missed that ... is that why you 'down-voted' ?

It is not true that fileis a reserved word in python. file is the name of a standard type in python 2. Using file as a variable name in python 2 is like using list as a variable name. It is not an error, but it could be annoying to the programmer because it hides the built-in name. For python >= 2.7, the list of reserved words is available as

import keyword
print(keyword.kwlist)

Hey ... sorry I missed that ... is that why you 'down-voted' ?

It was not me as downvoted you :)

Hey ... sorry I missed that ... is that why you 'down-voted' ?

It was not me as downvoted you :)

Sorry ...

anyways ... it's good to hear @Gribouillis, Dani's 'Posting Maven' :)

Hey everyone,

Thank you so much for your input. The path for the file on my mac is correct. I even checked it by dragging the file into a terminal window to see the exact path. It just doesnt work on it. I tried the code on my virtual machine for windows and it worked perfectly. Thank you everyone again. This site is so helpful. Especially for us who are beginning programmers.

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.