I'm probably doing something very silly and basic, but I just can't get this bit of code to work. I have a text file that contains a list of more text files (log files) with the full path for them. I want to open the first file, grab the list and then open each in turn (ultimately to do a search within each for errors) and then close them. The problem I am having is that I can't get the data from the newly opened secondary files to display.
Text file 1 (logs.txt) :
//server-1/program/data/instances/devapp/log/audit.log
//server-2/program/data/instances/devapp/log/bizman.db.log
The code I am trying to run:
import os
logdir = '/cygdrive/c/bob/logs.txt'
load_log_file = open (logdir, 'r')
read_log_file = load_log_file.readlines ()
def txt_search (read_log_file) :
for entry in read_log_file :
view_entry = open (entry, 'a+wb')
print view_entry
print txt_search (read_log_file)
The output looks like the following:
$ python log_4.py
<open file '//server-1/program/data/instances/devapp/log/audit.log
', mode 'a+wb' at 0xfff3c180>
<open file '//server-2/program/data/instances/devapp/log/bizman.db.log
', mode 'a+wb' at 0xfff3c1d8>
None
Any help would be greatly appreciated as I'm getting to the point of pulling my hair out!
Many thanks,
Bob
If you want to display the contents of the file then use view_entry.read(). You're just referencing the object hence why you're getting that response.
http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
The return type of open() is a file object. So when you print view_entry you are basically printing the description of the file object, not the content itself. Try this instead:
Your object
view_entry
refers to a file-object, not the contents of the file. Short answer, you need to read from theview_entry
.I would restructure the code as such:
I have also corrected the mode you are opening the file, as 'a+wb' does not make any sense for me (
a
is for appending,+
for updating,w
opens for writing and truncates the file, andb
is for binary mode).You can do something like this:
If you want to
print
the files as seen, so without the surrounding brackets and other list markup, you can use the following line: