kozm
Posts: 12
Joined: Wed Apr 22, 2015 3:41 pm

give variable as name

Fri Apr 24, 2015 6:11 pm

i'm struggling with my first python script, please help me if you can.
it is an infinite loop, that runs a program. the program with the command - sudo ~/hdd -r(report function) /my/cloud/folder - creates a report file about the condition of the hdd. (I'm using an old hdd to backup my cloud so the report file will be automatically uploaded to the cloud, this way i can check is the hdd still reliable or not.) I would like to name the report txt file with the date, when it was written.

act = datetime.date.today()
while True:
os.system("sudo ~/hdd -r /my/cloud/folder/act.txt") #this is a command that runs the program which writes the file!

time.sleep(14400)

kozm
Posts: 12
Joined: Wed Apr 22, 2015 3:41 pm

Re: give variable as name

Fri Apr 24, 2015 6:24 pm

act = datetime.date.today() < will be inside the loop

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: give variable as name

Fri Apr 24, 2015 6:27 pm

You need to create the file "act.txt" then write the date/time data to it.
Do a bit of googling about python and files, you'll find loads of examples.

Dave.
Apple say... Monkey do !!

User avatar
Laurens-wuyts
Posts: 716
Joined: Wed Aug 21, 2013 7:35 pm
Location: Belgium
Contact: Website

Re: give variable as name

Fri Apr 24, 2015 6:28 pm

I think you can do it like this:

Code: Select all

while True:
    act = datetime.date.today() 
    os.system("sudo ~/hdd -r /my/cloud/folder/" + act + ".txt") 

    time.sleep(14400)
;)

Laurens

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: give variable as name

Fri Apr 24, 2015 6:29 pm

Try

os.system("sudo ~/hdd -r /my/cloud/folder/" + act + ".txt")

Or if act isn't a string str(act)

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: give variable as name

Fri Apr 24, 2015 6:54 pm

What a horribly complex way to do it.

Why not use a /etc/cron.daily script?

sudo nano /etc/cron.daily/hdd

Code: Select all

#!/bin/bash
DATE=$(date +%d-%m-%y)
/home/pi/hdd -r "/my/cloud/folder/"$DATE".txt"
sudo chmod 755 /etc/cron.daily/hdd
sudo /etc/init.d/cron reload
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

kozm
Posts: 12
Joined: Wed Apr 22, 2015 3:41 pm

Re: give variable as name

Tue Apr 28, 2015 9:54 am

Thank you everyone! With your help it works now! :twisted:

Return to “Python”