I run a cronjob-script as root user. The script is located in /root/scripts/myscript.py
In this python script I write to a file like this
log = open(os.path.basename(__file__) + ".log", "w+")
print("some text", file=log)
This is supposed to create a file with the name myscript.py.log and it does. But this file is located in /root/myscript.py.log. Why is that? I expected it to be created in the script directory.
I use Debian Squeeze and add the job via crontab -e as root user.
This is because it depends on the cwd when the cron task was started, cron could change directory too so Always use absolute pathnames for jobs run by cron, or pass in an absolute stub from the command line.
In your case crontab -e as root, cron will run your script as root and therefore use the home directory for that user. ie /root