Opening text files from a list within another text

2019-08-06 16:16发布

问题:

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

回答1:

You can do something like this:

logdir = r"/cygdrive/c/bob/logs.txt"

with open(logdir) as fin:
    for line in fin:
        with open(line.strip()) as log:
            print log.readlines()

If you want to print the files as seen, so without the surrounding brackets and other list markup, you can use the following line:

print "".join(log.readlines())


回答2:

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.

C:\Users\brayden>python
Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test.txt', 'r')
>>> print f
<open file 'test.txt', mode 'r' at 0x003A6CD8>
>>> print f.read()
line1
line2
asdf

http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects



回答3:

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:

...
view_entry = open (entry, 'a+wb')
print (view_entry.readlines())
...


回答4:

Your object view_entry refers to a file-object, not the contents of the file. Short answer, you need to read from the view_entry.

I would restructure the code as such:

def error_search(logfile):
  ''' 
  This function retrieves a file-object, that is readable.
  Searches for a line containing the substring "ERROR", and returns True if it finds it.
  '''
  for line in logfile:
    if 'ERROR' in line:
      return True
  return False

def txt_search (read_log_file) :
  for entry in read_log_file :
    view_entry = open(entry, 'r')
    if os.path.exists(entry):
      has_error = error_search(logfile)
    if has_error:
      print entry, "has error!"
    else:
      print entry, "is error-free."


txt_search (read_log_file)

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, and b is for binary mode).