I’m trying to run the following script :
#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()
When I execute the above code, using F5 key, the result is “log something” is written on the shell, but there is no file created and there is nothing written onto the file if it exists already.
Please help.
You masked your typo by using a plain except statement. If you want to check for the file not being written in the try statement use something like except IOError.
A working logging.py (for those who did not see the error in the example):