Guyz,我遇到了以下错误与belwo代码,哪里出错? 任何清理建议,也接受以下UnboundLocalError:分配之前引用的局部变量'文件'
for line in file(timedir + "/change_authors.txt"):
UnboundLocalError: local variable 'file' referenced before assignment
代码:
import os,datetime
import subprocess
from subprocess import check_call,Popen, PIPE
from shutil import copyfile,copy
def main():
#check_call("ssh -p 29418 review-droid.comp.com change query --commit-message status:open project:platform/vendor/qcom-proprietary/radio branch:master | grep -Po '(?<=(email|umber):)\S+' | xargs -n2")
global timedir
change=147441
timedir=datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
#changeauthors = dict((int(x.split('/')[3]), x) for line in file(timedir + "/change_authors.txt"))
for line in file(timedir + "/change_authors.txt"):
changeauthors = dict(line.split()[0], line.split()[1])
print changeauthors[change]
try:
os.makedirs(timedir)
except OSError, e:
if e.errno != 17:
raise # This was not a "directory exist" error..
with open(timedir + "/change_authors.txt", "wb") as file:
check_call("ssh -p 29418 review-droid.comp.com "
"change query --commit-message "
"status:open project:platform/vendor/qcom-proprietary/radio branch:master |"
"grep -Po '(?<=(email|umber):)\S+' |"
"xargs -n2",
shell=True, # need shell due to the pipes
stdout=file) # redirect to a file
if __name__ == '__main__':
main()
谢谢,是行Gerritauthors = Dict(line.split()[0],line.split()[1])有问题..基本上我试图根据空间拆分每一行并保存第一个字符串作为索引,第二个字符串作为元素... – user1927396
@ user1927396您可以做'index,element = line.split('',1)'来实现这一点。 –
如果我这样做,我不断得到TypeError:预计最多1个参数,在行changeauthors = dict(索引,元素)得到2 – user1927396