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
#2
The modern way to do it (f-string syntax):

command = f'echo 0 | fbink -i {hour}.png -g x=100 -c'
The backward compatible (to 3.5 and earlier) way to do it (the format method of strings):

command = 'echo 0 | fbink -i {}.png -g x = 100 -c'.format(hour)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks! My code looks much better now.
Reply
#4
and also subprocess.run is preferd over os.system
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
That's working well,however when there is more text following the braces, it is ignored.
For example:
command = 'fbink -t regular=/mnt/us/clock/bahnschrift.ttf,top=1410,px=35,format "BATTERY {}%" -me'.format(batpercent)
The % (or any other text) is not displayed.
?
Reply
#6
I don't know, it's working fine for me. What is the value of batpercent? It's not a string with a new line character in it, is it?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
no, it's an integer...
Reply
#8
Turns out I was incorrect; it was a string with a trailing LF.
Reply
#9
(Dec-21-2019, 09:24 AM)buran Wrote: and also subprocess.run is preferd over os.system

I am running on python v2.7; subprocess.run is not available. Would subprocess.Popen also be preferred?
Reply
#10
End of life (support) for Python 2.7 is this coming Wednesday. Switch to Python 3.x if at all possible.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 749 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  How to write a part of powershell command as a variable? ilknurg 2 1,084 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Rename part of filename in multiple files atomxkai 7 7,217 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Append files and add column with last part of each filename NiKirk 0 2,564 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  How to include input as part of variable name Mark17 4 2,444 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,286 May-07-2021, 12:39 AM
Last Post: MDRI
  Print part of a variable rs74 4 2,524 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