好的,我是从 Perl 转向 Python 的,而且我在 Python 方面没有太多经验,所以这对于所有对 Python 更有经验的人来说似乎相当明显。
无论如何,我只是加载一个配置文件,然后作为自检打印加载的值。代码如下:
#!/home/y/bin/python2.7
import logging
import sys
import datetime
import yaml
def main(self):
#initialize the logger
logger = logging.getLogger(self.granularity)
log_fh = logging.FileHandler(filename='/home/logs/pipelineTest/pipelineTest' + datetime.datetime.now().strftime('%Y%m%d_%H%M') + '.log', mode='w')
logger.addHandler(log_fh)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
log_fh.setFormatter(formatter)
#read configuration file
if sys.argv[1]:
ymlFH = open(sys.argv[1])
else:
ymFH = open('/home/conf/pipelineTest/runPipeline.yml')
confDict = yaml.load(ymlFH)
if __name__ == '__main__':
#self-test code
for key, value in confDict.iteritems():
print 'Key is: ' + key + '\n'
print 'value is: ' + confDict[key] + '\n'
我遇到的错误是:
Traceback (most recent call last):
File "./runPipeline.py", line 30, in <module>
for key, value in confDict.iteritems():
NameError: name 'confDict' is not defined
我将其解释为名称“confDict”已超出范围。我不明白为什么它超出了范围。