re.search in Python yields "none" when using a file as the input
Question: I am very new to stack overflow and to programming in general, and appreciate
any help you can provide!

I want to use the re.search function in Python to find text in a file (this is
just practice, because ultimately I want to use re.sub and regular expressions
to find/replace). But I can't get it to work!

For example, if I enter the following code into Python:

    
    
    import re
    SearchStr = 'world'
    Result = re.search(SearchStr,"Hello world")
    print Result
    

I get the following output: `<_sre.SRE_Match object at 0x106875e00>` Great!

But then I made a file called "python_test.txt" which contains the text "Hello
world", and I ran the following script:

    
    
    import re
    InFileName = 'python_test.txt'
    InFile = open(InFileName, 'r')
    SearchStr = 'world'
    Result = re.search(SearchStr,InFile)
    print Result
    

I get the following output: `None`

If I replace the last three lines with `InFile.read()`, I get `'Hello
world.\n'` as the output, so I think my script is reading the file just fine.
I have also tried using `InFile.read()` instead of `InFile` in my `re.search`
terms; that didn't work either. Why doesn't my script find "world" in my file?


Answer: `re.search` expects string as the second argument. Not the file. Try use:

    
    
    Result = re.search(SearchStr,InFile.read())
    

