python打开文件、文件夹窗口、终端窗口

简介

在一些项目中,我们会需要在生成文件后打开某些文件或者文件夹窗口,这就需要使用到内置的文件打开方式了。

打开文件文件

Windows

import os
import subprocess


# 打开文件或者速度最快, 推荐,不过只适用于Windows
def start_file(file_path):
    os.startfile(file_path)


# 打开文件文件夹,到对应文件或者文件夹时只会选中,不会进入到内部, 只适用于Windows
def start_file2(file_path):
    subprocess.Popen(f'explorer /select,"{file_path}"')


# 与第一种功能类似,只适用于Windows
def start_file3(file_path):
    # os.system(f"start {file_path}")
    subprocess.Popen(f"start {file_path}", shell=True)


file_path = r"D:\1.gif"
# start_file(file_path)
# start_file2(file_path)
start_file3(file_path)

Linux

os.system('xdg-open "%s"' % foldername)

打开终端并输入内容

Linux

os.system("gnome-terminal -e 'ls'")

Windows

os.system("start powershell.exe cmd /k 'dir'")

引号中内容为执行的命令

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...