Logging module script: using os.getenv

Asked

Viewed 102 times

0

I am learning Python and am following the book A byte of python. In an exercise talking about the logging module there is the following line of code:

if platform.system().startswith('Windows'):
logging_file = os.path.join(os.getenv('DIRETORIOINICIAL'), os.getenv('PATH'), 'test.log')

The script just writes inside the file teste.log with the date and time the script was run.

I followed exactly as it is in the book but I always get the same trackback:

Traceback (most recent call last):
  File "using_logging.py", line 6, in <module>
    logging_file = os.path.join(os.getenv('DIRETÓRIOINICIAL'), os.getenv('PATH'), 'test.log')
  File "C:\Python34\lib\ntpath.py", line 108, in join
    result_drive, result_path = splitdrive(path)
  File "C:\Python34\lib\ntpath.py", line 159, in splitdrive
    if len(p) > 1:
TypeError: object of type 'NoneType' has no len()

It does not return or write the file.

  • What problem is occurring?

  • trackback returns this error but does not create the file or write. I change the ('DIRETÓRIOINICIAL') by a complete way? But from what I understood the doc says os.getenv takes the environment variable so I wouldn’t need it right?

  • Testing os.getenv('DIRETÓRIOINICIAL') and see if it returns any results.

  • None, then probably the problem is that my initial directory not this set is this?

  • Right, as a matter of fact 'DIRETÓRIOINICIAL' should exist in your Windows environment variables, review the example used is looking like a translation.

  • Even though %HomeDrive% still shows None

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.