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])
