Python: 如何在 Windows Explorer 上打开文件夹(Python 3.6.2, Windows 10)

Python: How to open a folder on Windows Explorer(Python 3.6.2, Windows 10)

如果我将要打开的路径存储在名为 finalpath 的字符串中,它看起来像这样: "./2.8 Movies/English/Die 硬系列"

那我如何在 Windows 资源管理器中打开它?(Windows 10)(Python 3.6.2)

P.S我知道很多人问过这个问题,但我没有找到他们清楚的。请尽快回答。

我找到了一个简单的方法。

import os
path = "C:/Users"
path = os.path.realpath(path)
os.startfile(path)

其他选择

import webbrowser, os
path="C:/Users"
webbrowser.open(os.path.realpath(path))

或单独使用 os

import os
os.system(f'start {os.path.realpath(path)}')

或子进程

import subprocess,os
subprocess.Popen(f'explorer {os.path.realpath(path)}')

subprocess.run(['explorer', os.path.realpath(path)])

跨平台:

import webbrowser


path = 'C:/Users'

webbrowser.open('file:///' + path)
import os

path = "C:\Users"


def listdir(dir):
    filenames = os.listdir(dir)
    for files in filenames:
        print(files)


listdir(path)

好的,这是另一个小菜一碟,它列出了目录中的所有文件

Windows:

import os
path = r'C:\yourpath'
os.startfile(path)

此方法是已批准答案的简化版本。