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
  How to include input as part of variable name Mark17 4 394 Oct-01-2021, 06:45 PM
Last Post: Mark17
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 258 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  [solved] subdictionaries path as variable paul18fr 4 753 May-18-2021, 08:12 AM
Last Post: DeaD_EyE
  Subprocess.Popen() not working when reading file path from csv file herwin 13 2,163 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 1,606 Jan-31-2021, 11:26 AM
Last Post: hcccs
  Can I include text using artist? tetrisbot 0 555 Aug-13-2020, 08:13 PM
Last Post: tetrisbot
  PyDrive download file path MiniMinnow 0 1,714 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow
  String to File Path creedX 4 1,252 Apr-06-2020, 07:29 PM
Last Post: creedX
  How to include pandas with pyinstaller Rickus 1 4,152 Feb-19-2020, 08:01 PM
Last Post: snippsat
  Add path to a local file in pop-up field pythonscripting 1 765 Feb-08-2020, 10:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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