1

I'm trying to use the logging module in Python. This is my simple script

import logging

logging.basicConfig(filename = 'logging.log', format = '%(message)s', filemode = 'a', level = logging.DEBUG)
logging.info('some info')

It works when I run the script from a Python editor (I use Spyder), but it doesn't work if I try to run the script from terminal with:

python ~/PYTHON/untitled0.py

untitled0.py is the name of the script in my PYTHON folder. Am I doing something wrong?

I use Python 2.7.2+ with Ubuntu 11.10

3
  • 1
    Where have you checked for your logging.log file? ~/PYTHON? Or somewhere else? Commented Feb 7, 2012 at 17:18
  • To clarify: your log file should be on the current directory, not in ~/PYTHON. Commented Feb 7, 2012 at 17:25
  • yes, I was checking in the wrong folder. Thank you! Commented Feb 7, 2012 at 17:41

2 Answers 2

2

The mistake i see here is you have to check the directory path where you are executing this script as the logging.log is created there.

Ex: $ python ~/PYTHON/untitled0.py

$ cat logging.log

and not

cat ~/PYTHON/logging.log
Sign up to request clarification or add additional context in comments.

Comments

0

This seems to be a common mistake when people work up a script and then start invoking it in some "different way" (e.g. shortcut icon, scheduler, shell script etc).

If you are unclear about what is going on with your script try cutting and pasting this to the top of your script or replacing your (backed up) script with this source:

import sys
import os

print "argv: %r"%(sys.argv,)
print "dirname(argv[0]): %s"%os.path.abspath(os.path.expanduser(os.path.dirname(sys.argv[0])))
print "pwd: %s"%os.path.abspath(os.path.expanduser(os.path.curdir))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.