Python Forum
How to use a variable as part of a filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use a variable as part of a filename
#1
I would like to simplify the following code, replacing the multiple if/elif's with a single line, but I have not been able to successfully figure it out.
def print_time():
    now = datetime.datetime.now()
    hour = now.hour
    minute = now.minute
    second = now.second
    if hour == 1:
        command = 'echo 0 | fbink -i 1.png -g x=100 -c'
    elif hour == 2:
        command = 'echo 0 | fbink -i 2.png -g x=100 -c'
    elif hour == 3:
        command = 'echo 0 | fbink -i 3.png -g x=100 -c'
    elif hour == 4:
        command = 'echo 0 | fbink -i 4.png -g x=100 -c'
    elif hour == 5:
        command = 'echo 0 | fbink -i 5.png -g x=100 -c'
    elif hour ==6:
        command = 'echo 0 | fbink -i 6.png -g x=100 -c'
#   etc...        
    os.system(command)
Any help is much appreciated!
Reply


Messages In This Thread
How to use a variable as part of a filename - by handyguy - Dec-21-2019, 02:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 856 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  How to write a part of powershell command as a variable? ilknurg 2 1,133 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Rename part of filename in multiple files atomxkai 7 7,380 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Append files and add column with last part of each filename NiKirk 0 2,631 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  How to include input as part of variable name Mark17 4 2,528 Oct-01-2021, 06:45 PM
Last Post: Mark17
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,346 May-07-2021, 12:39 AM
Last Post: MDRI
  Print part of a variable rs74 4 2,609 Jul-27-2020, 04:39 PM
Last Post: rs74

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020