dr.umma
dr.umma

Reputation: 37

python logging does not work from terminal

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

Upvotes: 1

Views: 2036

Answers (2)

Phil Cooper
Phil Cooper

Reputation: 5877

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))

Upvotes: 0

Anil Muppalla
Anil Muppalla

Reputation: 414

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

Upvotes: 2

Related Questions