0

I would like to redirect the print statements of my output to a text file. I have imported the sys and tried below.

import pprint
import os
import math
import sys

class ExportLimits(object):
    MMAP_ITER_TRIPS = 'manpower_mappings.map_iterator_trips'

def __init__(self, workset, crew_type, bid_period, divisor_files=None):
    log_file = "/opt/test/test_user/data/ESR_DATA/etab/slogfile.txt"
    sys.stdout = open(log_file, 'w')
    self.workset = workset
    self.crew_type = crew_type
    self.bid_period = bid_period
    self.tm = workset.getTM()
    self.crew_bid_period = self.crew_type + "+" + self.bid_period
    self.bid_period = self.tm.table('qf_user_bid_period')[(self.crew_bid_period)]
    self.period = Period(self.bid_period.bpstart, self.bid_period.bpend)
    self.filter_matcher = self._get_filter_matcher()
    self.iterator_trips = rave_api.eval(\
            ExportLimits.MMAP_ITER_TRIPS)[0]
    self.divisor_reader_lh = divisor_reader_lh.DivisorReader(\
            divisor_files=divisor_files)
    self.divisor_reader_sh = divisor_reader_sh.DivisorReader(\
            divisor_files=divisor_files)
    self.pp_start = self.period.getStart()
    self.pp_end = self.period.getEnd()

def export_limits(self, item_type):
    if item_type == 'DKSH':
       self._mandays_limits(SLKHH_GROUPS)
    else:
       self._mandays_limits(LAJSDLH_GROUPS)
def _mandays_limits(self, groups):
  crews = [self.tm.table('crew')[('99172447',)],
             self.tm.table('crew')[('7654678',)]]
  generator = ((crew, self.filter_matcher.getFilterNamePeriodsMap(crew.id))
                 for crew in self.tm.table('crew'))

  minres = defaultdict(lambda :RelTime(0))
  maxres = defaultdict(lambda :RelTime(0))

  for crew, group_to_periods in generator:
      print crew, group_to_periods
      try:
         crew_filter, period = group_to_periods.iteritems().next()
      except StopIteration:
         continue
      if crew_filter not in groups:
         continue

It works partially for me. I am able to print few of the lines, but not completely. Please find the below output of my log file where it has only printed fewer lines but not the complete logs. For some reason, it hasn't printed completely. (Please see the last line of the log file where it printed only till "alia".)

Log File:

crew _id="133245" id="176543" empno="8761890" sex="M" birthday="19681217" name="MICHEAL" forenames="LUCAS" maincat="C" preferredname="ESWAR" initials="LL" joindate="20010910 00:00" comjoindate="20010910 00:00" _void="title,logname,si,bcity,bstate,bcountry,alias,comenddate" {'X-SYD-BB-AUSLLH': [26JUN2017 00:00-21AUG2017 00:00]}

crew _id="214141" id="132451" empno="145432" sex="M" birthday="19630904" name="ESWARF" forenames="FJDJSK" maincat="C" preferredname="ESWADF" initials="WL" joindate="20010910 00:00" comjoindate="20010910 00:00" _void="title,logname,si,bcity,bstate,bcountry,alia

~
~

Please check and advise.

rawse
  • 5
  • 2
  • 1
    check out the python logging module! https://docs.python.org/3/library/logging.html – caffreyd May 22 '17 at 03:41
  • Have you tried touching the buffering argument in `open`? https://docs.python.org/3/library/functions.html#open – grovina May 22 '17 at 03:49
  • @grovina: I haven't tried the buffering argument. I can try that and let you know in a while. – rawse May 22 '17 at 04:00
  • 1
    @grovina: It works :-) Thanks for your inputs. – rawse May 22 '17 at 05:35
  • This seems to be a duplicate of http://stackoverflow.com/questions/4110891/python-how-to-simply-redirect-output-of-print-to-a-txt-file-with-a-new-line-crea . See also the other solutions there - IMO using the print function with a logfile argument is the simplest way to do this. Another useful tool in conjunction with this is functools.partial. With that you can make a partial function of print with the logfile set, and then use that function everywhere in your program, e.g. `print_to_log = functools.partial(print, file="my_log_file.txt")` ... `print_to_log("hello world")` – Michel Müller May 22 '17 at 05:38

1 Answers1

0

Instead of using sys.stdout you can write like:

output_file = open(log_file, 'w')

output_file.write('testing asdasdfsd')

Or if you want to write all kinds of print value in log file then :

output_file = open(log_file, 'w')

sys.stdout = output_file

that's it.

sachin
  • 379
  • 3
  • 16