세 개의 파일과 파일이 있는지 확인하기 위해 아래 코드를 작성했습니다. 파일에 "스캔"을 실행합니다 (파일이없는 경우). 존재하는 경우, 사용 가능한 파일에 대해 "스캔"을 실행하고 사용 가능한 파일에 적절한 출력 파일을 생성하는 것에 대해 걱정하지 마십시오.Python : 하나의 함수에서 하나의 함수에서 변수를 호출했지만 전역 변수를 사용하지 않음
내가 일하고 있어요 프로그램은 다음과 같은 코드가 포함이 세 개의 파일이 존재하는 경우
def InputScanAnswer():
scan_number = raw_input("Enter Scan Type number: ")
return scan_number
이 기능 검사 그렇다면, hashcolumn
에 특정 값을 할당에 filepathNum
def chkifexists():
list = ['file1.csv', 'file2.csv', 'file3.csv']
for filename in list:
if os.path.isfile(filename):
if filename == "file1.csv":
hashcolumn = 7
filepathNum = 5
if filename == "file2.csv":
hashcolumn = 15
filepathNum = 5
if filename == "file3.csv":
hashcolumn = 1
filepathNum = 0
#print filename, hashcolumn, filepathNum
def ScanChoice(scan_number):
if scan_number == "1":
chkifexists()
onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) #this is what is giving me errors...
elif scan_number == "2":
print "this is scan #2"
elif scan_number =="3":
print "this is scan #3"
else:
print "Oops! Invalid selection. Please try again."
def onlinescan(FileToScan, ResultsFile, hashcolumn, filepathNum):
# web scraping stuff is done in this function
내가 만나는 오류는 global name 'filename' is not defined
입니다. 로컬 변수를 chkifexists()
에서 onlinescan()
매개 변수로 보내려고한다는 점을 알고 있습니다. 나는 chkifexists()
함수의 끝에서
return filename
return hashcolumn
return filepathNum
를 사용하여 시도하지만, 그 중 하나가 작동하지 않았다. 글로벌 변수를 사용하여없이 나는
onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum)
라인 에서 할 노력하고있어 무엇 어쨌든이 있나요? 나는 그들이 낙담하고 다른 길로 갈 수 있기를 바랍니다. 또한 onlinescan()
에 hashcolumn
및 filepathNum
매개 변수가 있습니까?
고마워요, 이것은 제가 기대했던 것처럼 정확하게 작동했습니다! – serk
주문은 어떻게됩니까? 'chkifexists() = filename, hashcolumn, filepathNum'도 될 수 있을까요? – serk
당신이 통역을 시도하기는 쉽지만 충분히 기분이 좋으므로 대답을 드리겠습니다 : 아니오, SyntaxError : 함수 호출에 할당 할 수 없습니다. 어느 순간, 당신이 그것을 잠깐 생각하면, 많은 의미가 있습니다. – Nate