简体   繁体   English

将stdout重定向到Python中的文本文件?

[英]Redirect stdout to a text file in Python?

I would like to redirect the print statements of my output to a text file. 我想将输出的打印语句重定向到文本文件。 I have imported the sys and tried below. 我已经导入了sys并在下面尝试过。

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".) (请参阅日志文件的最后一行,该行仅打印到“ 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]} 乘员组_id =“ 133245” id =“ 176543” empno =“ 8761890” sex =“ M” Birthday =“ 19681217” name =“ MICHEAL” forenames =“ LUCAS” maincat =“ C” preferredname =“ ESWAR”首字母=“ LL “ joindate =” 20010910 00:00“ comjoindate =” 20010910 00:00“ _void =”标题,登录名,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 乘员组_id =“ 214141” id =“ 132451” empno =“ 145432” sex =“ M” Birthday =“ 19630904” name =“ ESWARF” forenames =“ FJDJSK” maincat =“ C” preferredname =“ ESWADF”首字母=“ WL “ joindate =” 20010910 00:00“ comjoindate =” 20010910 00:00“ _void =”标题,登录名,si,bcity,bstate,国家,别名

~
~

Please check and advise. 请检查并告知。

Instead of using sys.stdout you can write like: 不用sys.stdout,您可以这样写:

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') output_file =打开(log_file,'w')

sys.stdout = output_file sys.stdout =输出文件

that's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM