我正在處理一個腳本來調用I/O文件的可執行文件。我正在使用子進程,並試圖將exe和stdout放到日誌文件中。問題是我想輸出一個簡單的ASCII文件,我得到一個十六進制文件。只是真的學習編程python(或任何語言),所以我假設有一些我可以做的格式,但我不明白。我在本網站和其他網站上進行了一些搜索,但是我沒有像使用子流程那樣的東西。 「outRadcorr」是我最需要的幫助......任何想法?按要求提供更多代碼。Python:將stdout寫入日誌文件;輸出是十六進制不ascii
導入系統模塊
import os, sys, string, traceback, time, datetime
import params
from subprocess import Popen, PIPE, STDOUT
...一些代碼在這裏.....
寫入標準輸出到日誌文件
rad_log_file = open(dsFolder + '\\radcorr.log', 'w')
# loop through the files in raw file list to run radiometric correction
for rawfiles in rawFolderList:
# Define the file base
rawBase = rawfiles.split(".")[0]
print ('\nProcessing file: %s \n')%(rawBase)
# define variables from raw file to process radcorr
radFile = rawfiles
pixFile = ('%s.pix')%(rawBase)
attFile = ('%s.att')%(rawBase)
# create windose bat file function
def rad_bat_writer(radcorr_bat):
with open(dsFolder + '\\radcorr.bat', 'a') as rad_bat_file:
rad_bat_file.write(radcorr_bat + '\n')
# grab radcor input/output files and run radcorr.exe
radcorr = ('C:\\itres\\bin\\radcorr.exe -f 1 -j 100 -g 50 -s -1 -n -1 %s %s %s -i '+ rawFolder + '\%s,rb -o ' + radFolder + '\%s -a ' \
+ radFolder + '\%s -c C:\\itres\\rad_cal_files\\%s -I 0 -v 0 -r Y -R Y -^^ 2') %(sum,scatter,shift,radFile,pixFile,attFile,rad_prefix)
# print out radcorr command
print radcorr
# Execute radcorr and write stdout
outRadcorr = Popen("{};".format(radcorr), stdout=PIPE, stderr=STDOUT)
for line in outRadcorr.stdout:
sys.stdout.write(line)
rad_log_file.write(line)
# write output to log
rad_bat_writer(radcorr)
# Close out exe and log files
outRadcorr.wait()
rad_log_file.close()
我很困惑... radcorr.exe輸出二進制到標準輸出,你想改變它爲文本格式?什麼是在屏幕上打印? ...和rad_log_file不是文本? – tdelaney 2014-09-10 22:29:35
您將同樣的事情寫入屏幕和日誌文件。如何查看日誌文件以查看它是「十六進制」的。 「十六進制」這個詞是什麼意思? – tdelaney 2014-09-10 23:18:44
使用'r'C:\ itres \ bin \ radcorr.exe -f 1 -j 100 -g 50 -s -1 -n -1 {sum} {scatter} {shift} -i {radpath},rb -o {pixpath} -a {radFolder} \ {attFile} -c {rad_prefix_path} -I 0 -v 0 -r Y -RY - ^^ 2'.format(** vars())',其中'radpath = os。 path.join(rawFolder,radFile)','pixpath = os.path.join(radFolder,pixFile)'和'rad_prefix_path = os.path.join(r'C:\ itres \ rad_cal_files',rad_prefix)''。 – jfs 2014-09-13 02:07:57