在Python中的特定网络位置打开Windows资源管理器

时间:2013-11-25 10:45:30

标签: python windows python-3.x explorer

我尝试了很多主题的变种,让这个浏览器窗口在P:\驱动器上打开,从我的小知识告诉我,文件夹的路径到处都是,但C:\驱动器意味着它失败(它适用于C :)所以也许路径错了?下面的代码显示了我所做的一些尝试,但仍然没有运气,“P:”在所有机器上映射相同。

def Open_Win_Explorer_and_Select_Dir():
    import subprocess
    fldrname = os.path.basename(currentproject.get())
    print(fldrname)
    #subprocess.Popen('c:\windows\EXPLORER.EXE', cwd=(P:/Projects 2013/)
    #subbprocess.Popen('c:\\windows\\EXPLORER.EXE' cwd=('P:\\Projects_2013\\')fldrname)
    #subprocess.Popen(r'C:/Windows/explorer.exe', cwd=r'//WRDBSVR/Project_Data/Projects_2013/'+fldrname)
    subprocess.Popen('explorer /n, /select r"\\192.168.0.27\\Project_Data\\Projects_2013\\"'+fldrname)
    #subprocess.Popen('explorer /n, /select r"P:\\Project_Data\\Projects_2013\\"'+fldrname)

3 个答案:

答案 0 :(得分:2)

以下应该做的工作。

import subprocess
subprocess.Popen('explorer "{0}"'.format(full_folder_path))

更新 -

在我的系统上测试 -

full_path = os.path.join("P:/Project_Data/Projects_2013/",fldrname)
print full_path # Verify that it is correct
subprocess.Popen('explorer "{0}"'.format(full_path))

答案 1 :(得分:2)

打开我的电脑(用于Windows)试试:

import subprocess
subprocess.Popen('explorer ""')

"#if subprocess.Popen('explorer "{0}".format(full_path)') is struck at pc\my documents.
where full_path=os.path.join("your/path")"

答案 2 :(得分:1)

来自Ashish Nitin Patil的回答肯定更好,因为使用路径变量总是一个好主意,你的报价有问题:

# This line is not correct
'explorer /n, /select r"\\192.168.0.27\\Project_Data\\Projects_2013\\"'+fldrname
#                      ^you start a new string without ending previous one
# this one is correct
'explorer /n, /select ' + r'\192.168.0.27\Project_Data\Projects_2013\' + fldrname
#                     ^first ending string start

此外,使用原始字符串(r"xxx")意味着\不会转义字符,因此您不应将它们加倍。如果你想加倍它们,你不需要预先r

最后一句:在处理路径时要注意避免字符串连接(+);你应该使用os.path.join()代替。