0

Basically I want to do something like this:

def add_song():
    song = filedialog.askopenfilename(
        title="Choose a Song", filetypes=(("mp3 Files", "*.mp3"),))
    song = song.replace(string that means path, "")
    song_box.insert(END, song)

Instead of this:

def add_song():
    song = filedialog.askopenfilename(
        title="Choose a Song", filetypes=(("mp3 Files", "*.mp3"),))
    song = song.replace("C:/specifically/stated/path", "")
    song_box.insert(END, song)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Krizby
  • 1
  • 1
  • 2
    Are you asking how to extract the file name from a path? That's `os.path.basename`. – Tim Roberts Oct 16 '22 at 02:12
  • I am asking how to display just the file name of the song in a playlist box without having to type the specific path in the code for the player. – Krizby Oct 16 '22 at 02:20
  • I found an answer to what I needed to do in case anyone comes across this with the same question: – Krizby Oct 16 '22 at 02:35
  • `code` def add_song(): song = filedialog.askopenfilename( title="Choose a Song", filetypes=(("mp3 Files", "*.mp3"),)) song = song.split("/")[-1].split('.')[0] song_box.insert(END, song) `code` – Krizby Oct 16 '22 at 02:36
  • That's exactly the same as `os.path.basename`, which will work even with Windows filenames, where your code won't. – Tim Roberts Oct 16 '22 at 06:00

1 Answers1

0

I think you are asking how to extract the file name from a path. That's what os.basename is for.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30