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

在Python中的特定网络位置打开windows资源管理器,python,windows,python-3.x,explorer,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())

我已经尝试了很多不同的主题,在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)

下面应该做这项工作

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

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

要打开我的电脑(windows版),请尝试:


不幸的是,它无法工作,它在默认的“C:\my documents”文件夹中打开explorer:“subprocess.Popen('explorer“{0}.”format(“P:/Project\u Data/Projects\u 2013/”+fldrname))”和“subprocess.Popen('explorer“{0}”)format(\\wrdbsvr\\Project\u Data\\Projects\u 2013\\\\”+fldrname))都执行相同的操作,即
/n,/select
命令行中缺少
explorer
命令的选项,或者
explorer
不接受前斜杠
/
subprocess.Popen('explorer{0}')。format(full_folder_path))
工作,但是如何将此窗口置于其他窗口之上,如果说用户在程序中的某个地方执行了这条语句后转到其他窗口?Joel-是的,通过使用@ashish original idea和您对os.join的想法,我创建了这样一个窗口:“newdir=os.path.join”(“P:\\Projects\u 2013”,fldrname)subprocess.Popen('explorer“{0}”.format(newdir)),它很有效,非常感谢大家,是时候让我的头发长回来了。
subprocess.Popen('explorer“{0}”format(full_folder_path))
起作用了,但是如果用户在程序中的某个地方执行了此语句后转到其他窗口,我不知道如何才能做到这一点,但无论如何,您确定要这样做吗?当我刚刚选择另一个窗口时,系统弹出一个窗口时,我总是很恼火:-/Hahaha。是的,这很烦人,但对于我的申请,我有点要求这样做。
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")"