1

I have a python script at /home/pi/update_rrd_data.py that's supposed to store logs in the folder /home/pi/logs_rrd.

This is how I've implemented it:

current_folder = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
log_folder = os.path.join(current_folder, 'logs_rrd')
if not os.path.exists(log_folder): os.makedirs(log_folder)

file_name = __file__[:-3]  + '_' + datetime.strftime(datetime.now(), '%Y%m%d%H%M') + '.log'
log_file = os.path.join(log_folder, file_name)

logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(filename)s->%(funcName)s:%(message)s', datefmt='%m/%d/%Y_%I:%M:%S_%p')
logging.info('\nCurrent Folder: {}\nLog Folder: {}\nLog File: {}'.format(current_folder, log_folder, log_file))

When I run the script manually, it works fine and this is what I get in the log file at /home/pi/logs_rrd/update_rrd_data_201709271426.log:
Current Folder: /home/pi
Log Folder: /home/pi/logs_rrd
Log File: /home/pi/logs_rrd/update_rrd_data_201709271426.log

However, when I run it as a cron job, the log files are created in the same folder as the script instead of the logs_rrd folder. This is what I get in the log file at /home/pi/update_rrd_data_201709271445.log:
Current Folder: /home/pi
Log Folder: /home/pi/logs_rrd
Log File: /home/pi/update_rrd_data_201709271445.log

I can't figure out why the log folder value is ok but then the log file removes the logs_rrd part of log folder.

This is how I run the cron:

*/5 * * * * /usr/bin/python /home/pi/update_rrd_data.py

All the folders and files are owned by root so there shouldn't be any write permission issues.

Rahul
  • 371
  • 1
  • 2
  • 12
  • 1
    Did you try to use `__file__` instead `inspect.stack()[0][1]`? – amarynets Sep 27 '17 at 18:53
  • @AndMar Yes I did but then the Current Folder becomes /root. Also, the values of current folder and log folder are correct either way. It's when I try to combine the file name to the log folder that it acts weird and removes the "logs_rrd" part. – Rahul Sep 27 '17 at 18:58

1 Answers1

1

in the case where it doesn't work, __file__ is probably an absolute path, so

log_file = os.path.join(log_folder, file_name)

has no effect, (it retuns file_name) and the original script directory is kept.

Instead of __file__[:-3], just do:

os.path.basename(os.path.splitext(__file__)[0])

to get rid of the extension (which may not be .py in some cases BTW) and of the script file path in all cases, so the next os.path.join command works properly.

Alternate solution using pathlib and a Path object (requires Python 3):

pathlib.Path(__file__).with_suffix('').name

that removes the directory & the file extension amounting to the same result.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • 1
    I've become partial to `pathlib.Path`, so `p = pathlib.Path(__file__)` then simply `p.with_suffix('').name` *So* much more readable than `os.path.blahblahblah(os.path.bloop(path)[0])` – juanpa.arrivillaga Sep 27 '17 at 19:06
  • I always used `os.path`, but okay. – Jean-François Fabre Sep 27 '17 at 19:07
  • 1
    Same, it wasn't until recently that I started, but it has lots of wonderful features. Instead of `os.join`, *it overloads the `/` operator* (as well as making it available as a method)!!! Well worth checking out: https://docs.python.org/3/library/pathlib.html – juanpa.arrivillaga Sep 27 '17 at 19:08
  • had a quick look and it's really good. thanks for pointing this out. The `/` operator is a nice addition. – Jean-François Fabre Sep 27 '17 at 19:13
  • 1
    pathlib sounds cool but I should have mentioned I'm using python 2.7 so I can't use that.. – Rahul Sep 27 '17 at 19:24
  • @Jean-FrançoisFabre Thanks a lot for the answer! I should probably have printed the file_name variable in the log and that'd have made things much clearer. I have never used os.path.basename(os.path.splitext(__file__)[0]) and you're right sometimes the extension isn't .py (it's .py*) and I've had to add logic in my script to detect that. I'll update all my other scripts too now :) – Rahul Sep 27 '17 at 19:26
  • @juanpa.arrivillaga now I know why I'm not using `pathlib`: I'm stuck with python 2.7 compatibility at work :( – Jean-François Fabre Sep 27 '17 at 19:29