Ahmed Yasser
Ahmed Yasser

Reputation: 139

how to open multiple files in default program with python

We have the function os.startfile() where I can pass the path to file so it would be opened in the default program if any one declared.

Now I need to open multiple files at once , using a simple for loop would open each one in a new instance or would close the previous files .

So is there a functions to which I can pass a list of paths to be opened at once?

For declaration : I need to emulate clicking on a file then pressing control then clicking on other file and finally pressing enter or using the open "open with" in file explorer.

edit: here is a photo of what i need to emulate from file explorer enter image description here

edit: trying a simple for loop like this

import os
my_list = [ "D:\Music\Videoder\Abyusif I أبيوسف\ِAbyusif - Kol 7aga Bteb2a Gham2a l أبيوسف - كل حاجة بتبقى غامقة.mp3",
            "D:\Music\Videoder\Abyusif I أبيوسف\Abyusif- 3ala maydoub eltalg (Prod by NGM) l أبيوسف- على ما يدوب التلج.mp3"]

for song in my_list:
    
    os.startfile(song)

this would open the last element in the list , as after opening each file the previous is closed

Upvotes: 1

Views: 1623

Answers (2)

ArKan
ArKan

Reputation: 485

Well, if you only want to execute music files, a quick and dirty way can be achieved like so:

import os
import time
from win32com.client import Dispatch

my_song_list = [
    r'path\to\my\awesome\music\1.mp3',
    r'path\to\my\awesome\music\2.mp3',
    r'path\to\my\awesome\music\3.mp3'
]
wmp = Dispatch('WMPlayer.OCX')
for i, path in enumerate(my_song_list):
    song = wmp.newMedia(path)
    os.startfile(path)
    if i < len(my_song_list) - 1:
        time.sleep(song.duration)

Using the Windows Media Player to get the song's duration, then, running the audio file (openig with default player), and waiting exactly the duration to run the next.

Upvotes: 1

KetZoomer
KetZoomer

Reputation: 2914

If I understood your question correctly, you could use a simple for loop.

Code:

import os

file_paths = ["<file path here>", "<file path here>"]
for file in file_paths:
    os.startfile(file)

Upvotes: 1

Related Questions