Please note: Any method which involves putting your login password in plain text, in a command or in a file, is insecure and should NOT be used!
The correct way to do it to setup
sudo such that only the one specific command you need, i.e.
echo date... > rtc..., is allowed to run WITHOUT needing the password.
Step 1. Create a shell script with just that command
Step 2. Set up sudo to allow
pydatertc.sh to execute without requiring a password
Step 3. Modify your python script to call
Now your script should run without requiring a password AND without compromising the security of your account, your data or your system!
Alternative only for
wakealarm (not for general use!):
In this specific case only, since the
/sys/class/rtc/rtc0/wakealarm file only controls the wake-up alarm for the system and is otherwise harmless, another alternative to avoid the password is either to take ownership of that file with
chown (if you are the only user setting the alarm), or make it world-writeable with
chmod +666; in that case, simply remove the
sudo from your Python call, leaving
sh -c "...." intact.