- 1 year ago
How can I open multiple files the same way explorer.exe does?
To open multiple files the same way "explorer.exe" does in Windows, you can use the os.startfile
function in Python. The os.startfile
function launches the specified file or folder using the default associated application, just like double-clicking on the file in File Explorer.
Here's how you can use os.startfile
to open multiple files:
pythonimport os
def open_files(file_paths):
for file_path in file_paths:
os.startfile(file_path)
# Example usage: Pass a list of file paths to open
file_paths = ["C:\\path\\to\\file1.txt", "C:\\path\\to\\file2.txt", "C:\\path\\to\\file3.docx"]
open_files(file_paths)
In the example above, the open_files
function takes a list of file paths as input and uses os.startfile
to open each file one by one. The function will open the files using the default associated application, just like when you double-click on the files in File Explorer.
Note that the behavior of os.startfile
may vary depending on the file type and the default applications set on the system. It will use the default application registered for the specific file extension to open the file. If you want to open the files with a specific application or program, you can use other methods like subprocess.Popen
with the appropriate application path and arguments.