가이아, belwo 코드에서 다음 오류가 발생합니다. 어디에서 ti가 잘못됩니까? 어떤 정리 제안도UnboundLocalError : 할당 전에 참조 된 로컬 변수 'file'
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 You 이를 달성하기 위해'index, element = line.split ('', 1)'을 할 수있다. –
내가 계속 그렇게한다면 TypeError : dict는 최대 1 개의 인수를 필요로하고, 2는 줄에 놓는다. changeauthors = dict (index, element) – user1927396