试图钻取我的驱动器上包含子文件夹的目录。当我找到具有我正在寻找的文件扩展名的文件时,我想要完整的文件路径。现在这就是我所拥有的:
import os
import Tkinter
import tkFileDialog
from Tkinter import Tk
from tkFileDialog import askopenfilename
root = Tkinter.Tk().withdraw()
dirname = tkFileDialog.askdirectory(initialdir='.')
list = []
for root, dirs, files in os.walk(dirname):
for name in files:
if name.find(".txt") != -1:
name = str(name)
name = os.path.realpath(name)
list.append(name)
print list
这是返回的
c:\users\name\desktop\project\file.txt
但是该 file.txt 位于
c:\users\name\desktop\project\folder1\file.txt