简体   繁体   English

如何使用python在默认程序中打开多个文件

[英]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.我们有函数os.startfile() ,我可以在其中传递文件的路径,因此如果有任何声明,它将在默认程序中打开。

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 .现在我需要一次打开多个文件,使用简单的 for 循环将在新实例中打开每个文件或关闭以前的文件。

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编辑:这是我需要从文件资源管理器中模拟的照片在此处输入图片说明

edit: trying a simple for loop like this编辑:尝试这样一个简单的 for 循环

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这将打开列表中的最后一个元素,因为在打开每个文件后,前一个元素被关闭

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.使用 Windows Media Player 获取歌曲的持续时间,然后运行音频文件(使用默认播放器打开),并准确等待持续时间运行下一个。

If I understood your question correctly, you could use a simple for loop.如果我正确理解你的问题,你可以使用一个简单的 for 循环。

Code:代码:

import os

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM