import os def listDir(curPath, pixLen): list = [] # print("当前路径:" + curPath) files = os.listdir(curPath) for path in files: fullPath = os.path.join(curPath, path) if os.path.isfile(fullPath): # append是打元素加到尾部 list.append(fullPath[pixLen:]) else: #extend是把列表中所有元素加到另一个列表 list.extend(listDir(fullPath, pixLen)) return list #print(__file__) ''' __file__表示当前文件名的绝对路径 ''' defaultDir = os.path.dirname(__file__); #取文件对应的目录 dir = input("输入游戏路径:\n") if dir == "": dir = defaultDir print("使用当前路径:", defaultDir) allFiles = listDir(dir, len(dir)) for path in allFiles: #input("按键继续") print(path)
标签:__,遍历,文件名,python,curPath,print,path,os,dir From: https://www.cnblogs.com/barrysgy/p/17761658.html