初学者打开资源管理器显示文件夹内容

2024-03-11

我几天来一直在 Windows 7 上修改 Python 3.66。取得了良好的进展, 但我完全不知道如何让 Windows 资源管理器打开并显示我想要的文件夹内容。

我已经尝试了至少 7 种不同的解决方案,但似乎都不起作用。他们都可以很好地打开资源管理器,但从来没有使用我的Folder_selected 多变的。

资源管理器位是代码的最后一行。 这是(我怀疑编码错误)来源:

#FRenum-v.04
#renumbers a folder of files to 01 onward preserving file extenders.
#Steve Shambles june 2018, my 2nd ever python program

from tkinter import filedialog
from tkinter import *
import os
import os.path
import subprocess

#user selects directory
root = Tk()
root.withdraw()                             #stop tk window opening
folder_selected = filedialog.askdirectory() #open file requestor

#change dir to folder selected by user,
os.chdir (folder_selected)
#path is now the dir
path=(folder_selected)

# read user selected dir   
files = os.listdir(folder_selected)

# inc is counter to keep track of what file we are working on
inc = 1 

for file in files:
    #store file extender in string file_ext
    file_ext = os.path.splitext(file)[1]

    # build new filename, starting with a "0" then 
    #value of inc then add file extender
    created_file=("0"+str(inc)+ file_ext)

    #if file does not exist rename it
    if not os.path.exists(created_file):
        os.rename(file,created_file)

        #next one please, until done
        inc = inc+1 #add to counter

#open explorer showing folder of renamed files   
subprocess.Popen(["C:\\Windows\\explorer.exe"]) 

#these do not work properly, opens in c: or my docs    
#subprocess.Popen(["C:\\Windows\\explorer.exe"+ folder_selected]) 
#subprocess.Popen(["C:\\Windows\\explorer.exe", folder_selected])    
#subprocess.Popen(["C:\\Windows\\explorer.exe","folder_selected"])    



#todo
#---------
#ignore sub-folders
#confirm requestor
#undo feature    
#find out how to stop dos box showing in compiles prg

所以,你的程序是完美的,只是由于某种原因在路径中使用了错误的斜杠,python 显然可以处理,但 explorer.exe 不能。
我运行了你的程序并打印出来folder_selected我得到了C:/Users/Michael/Desktop/test。其中包含正斜杠,并且 Windows 路径使用反斜杠。
我只想替换subprocess.Popen(["C:\\Windows\\explorer.exe"]) to: subprocess.Popen(["C:\\Windows\\explorer.exe", folder_selected.replace('/', '\\')])这将用反斜杠替换任何正斜杠,explorer.exe 应该能够处理。
希望这有效:)
Windows 路径中也不能包含任何类型的斜杠,因此用户不会拥有包含任何斜杠的目录/在其中,所以replace应该没事

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

初学者打开资源管理器显示文件夹内容 的相关文章

随机推荐