python打开文件、文件夹窗口、终端窗口
2022/7/21 1:23:40
本文主要是介绍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'")
引号中内容为执行的命令
这篇关于python打开文件、文件夹窗口、终端窗口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python