戳人痛处

导航

python在window下获取当前路径,打开文件夹并选定文件

cmd命令:

获取当前路径 echo %cd%

打开文件夹并选定文件 explorer /select,D:\file\.......

如下为实现代码,复制并保持为1.py文件看效果

import os
output = os.popen("echo %cd%")  #获取路径
file1=output.read().strip('\n')  #去掉换行后的路径
#print(file1+"233")
output1=os.popen("explorer /select,"+ file1+"\\"+"1.py") #打开文件夹并选定文件
#print(output1.read())
#print("explorer /select,"+ file1+"\\"+"1.py")

效果类似如下

 逛博客时,在博客https://www.cnblogs.com/lijunjiang2015/p/7689492.html 发现一个一个方法可以优化路径获取;

import sys
print(sys.argv[0])

 

posted on 2020-12-28 10:33  戳人痛处  阅读(1862)  评论(0编辑  收藏  举报