Python NameError:未定义'input_file'名称

时间:2017-03-06 21:37:45

标签: python

很抱歉,如果这是一个基本问题,但我从未接触过Python,但需要测试此脚本。运行时,我收到错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'input_file' is not defined
>>>

代码:

import gzip
import re
import sys
import ntpath


if len(sys.argv) != 2:
    print 'Usage: slt_cleanup.py <input_slt_file>'

input_file = sys.argv[1]
out_filename = 'EDITED_' + ntpath.basename(input_file)
print 'Input file is '+ input_file


def clean_result(matchobj):
    #print 'DEBUG: matchobj.group(1) --> ' + matchobj.group(1)
    result_content = matchobj.group(1)
    if len(result_content) > 64:
        result_content = result_content[:64]
    return 'result="' + result_content + '">'

with gzip.open(input_file) as f:
    curr_content = f.read()

    with gzip.open(out_filename, 'wb') as f_out:      
        curr_content = re.sub(r'<ErrorMessage>','<ErrorMessage><![CDATA[', curr_content)
        curr_content = re.sub(r'</ErrorMessage>',']]></ErrorMessage>', curr_content)
        curr_content = re.sub(r'result="(.*)">', clean_result, curr_content)
        f_out.write(curr_content)
        print 'Output file written: ' + out_filename

0 个答案:

没有答案