Python:在Explorer / Nautilus / Finder中打开一个文件夹 [英] Python: Opening a folder in Explorer/Nautilus/Finder

查看:163
本文介绍了Python:在Explorer / Nautilus / Finder中打开一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Python,并且具有特定文件夹的路径。我想使用该系统的默认文件夹资源管理器打开它。例如,如果是Windows计算机,则要使用资源管理器;如果是Linux,则要使用Nautilus或那里的默认值;如果是Mac,则要使用Finder。

I'm in Python, and I have the path of a certain folder. I want to open it using the default folder explorer for that system. For example, if it's a Windows computer, I want to use Explorer, if it's Linux, I want to use Nautilus or whatever is the default there, if it's Mac, I want to use Finder.

我该怎么做?

推荐答案

我很惊讶没有人提到使用 xdg-open 用于* nix ,它将同时适用于文件和文件夹:

I am surprised no one has mentioned using xdg-open for *nix which will work for both files and folders:

import os
import platform
import subprocess

def open_file(path):
    if platform.system() == "Windows":
        os.startfile(path)
    elif platform.system() == "Darwin":
        subprocess.Popen(["open", path])
    else:
        subprocess.Popen(["xdg-open", path])

这篇关于Python:在Explorer / Nautilus / Finder中打开一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆