2012-03-08 5 views
0

나는 다음과 같은 스크립트를 실행하기 위해 노력하고있어 : 쉘에 기록 파이썬 2.7 수입 혼란

#Contents of logging.py 
import sys 
import os 


global gLogfile 
global gLogFileFlag 

#----------------------------------------------------------------------------------------- 
def initLogging(): 
    global gLogFileFlag 
    try: 
     glogFile = 'D:\logggggging.log' 
     print gLogFile 
     fileObject = open(gLogFile, 'w') 
     gLogFileFlag = True 
     fileObject.close() 
    except: 
     gLogFileFlag = False 



#----------------------------------------------------------------------------------------- 
def logIt(text): 
    sys.stdout.write(text) 
    if(gLogFileFlag): 
     hFile = open(gLogFile, 'a') 
     hFile.write(text) 
     hFile.close() 


#----------------------------------------------------------------------------------------- 


#contents of test_defualt.py 
from logging import initLogging 
from logging import logIt 

def main(): 
    initLogging() 
    logIt("log something") 

main() 

나는 F5 키를 사용하여, 위의 코드를 실행

는, 결과는 "뭔가를 기록"하지만,이 이미 생성 된 파일이 없으며 이미 존재하는 경우 파일에 아무 것도 기록되지 않습니다.

도와주세요.

+0

, 나는 오류를 실현했습니다. 변수를 편집하는 동안 이름 지정과 전역 키워드를 사용하지 않았습니다. 그것의 일 벌금. 고맙습니다. – Vinayaka

답변

1

일반 제외 문을 사용하여 오타를 숨겼습니다. try 문에 쓰여지지 않은 파일을 검사하려면 IOError와 같은 것을 사용하십시오.

(예에서 오류가 표시되지 않은 사람들을위한) 작업 logging.py은 : 정말 미안 해요

import sys 
import os 

#----------------------------------------------------------------------- 
def initLogging(): 
    global gLogFileFlag 
    global gLogFile 
    try: 
     gLogFile = './logggggging.log' 
     print gLogFile 
     fileObject = open(gLogFile, 'w') 
     gLogFileFlag = True 
     fileObject.close() 
    except IOError: 
     gLogFileFlag = False 

#------------------------------------------------------------------------ 
def logIt(text): 
    sys.stdout.write(text) 
    if(gLogFileFlag): 
     hFile = open(gLogFile, 'a') 
     hFile.write(text) 
     hFile.close()