Python Forum
How to include Variable in File Path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to include Variable in File Path
#1
hi guys!

Im struggling for play a sound with this command.

I need XXX become the current_sound value.

Can you help me? how you can see im very noob on python

current_sound = item_list[current_item_index]["som"] playsound("C:/Users/thoma/Desktop/test/XXX.mp3")

thanks!
Reply
#2
You should look into the format method of strings, or the even newer f-string syntax (Python 3.6+).

# format method
current_sound = playsound("C:/Users/thoma/Desktop/test/{}.mp3".format(item_list[current_item_index]["som"]))
# f-string
current_sound = playsound(f"C:/Users/thoma/Desktop/test/{item_list[current_item_index]['som']}.mp3")
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jan-05-2020, 12:53 AM)ichabod801 Wrote: You should look into the format method of strings, or the even newer f-string syntax (Python 3.6+).

# format method
current_sound = playsound("C:/Users/thoma/Desktop/test/{}.mp3".format(item_list[current_item_index]["som"]))
# f-string
current_sound = playsound(f"C:/Users/thoma/Desktop/test/{item_list[current_item_index]['som']}.mp3")

Thank your for the answer!

i've got an error: TypeError: 'module' object is not callable
Reply
#4
You are going to need to show more code and the full text of the error. Please use python and output tags when posting code and results. Here are instructions.
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
  import a function from another file using relative path paul18fr 6 3,270 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  How to include one script into another? MorningWave 8 2,075 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  working directory if using windows path-variable chitarup 2 1,524 Nov-28-2023, 11:36 PM
Last Post: chitarup
  how include a python code in notpad++ plugin akbarza 2 1,592 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Regex Include and Exclude patterns in Same Expression starzar 2 1,955 May-23-2023, 09:12 AM
Last Post: Gribouillis
  File path by adding various variables Mishal0488 2 4,084 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 7,411 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Script File Failure-Path Error? jerryf 13 8,012 Nov-30-2022, 09:58 AM
Last Post: jerryf
  How to include input as part of variable name Mark17 4 3,834 Oct-01-2021, 06:45 PM
Last Post: Mark17
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,221 Sep-09-2021, 01:25 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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