我刚刚开始使用 Python,但已经发现它比 Bash shell 脚本更加高效。
我正在尝试编写一个 Python 脚本,该脚本将遍历从我启动脚本的目录分支的每个目录,并且对于它遇到的每个文件,加载此类的实例:
class FileInfo:
def __init__(self, filename, filepath):
self.filename = filename
self.filepath = filepath
filepath 属性将是从根 (/) 开始的完整绝对路径。这是我希望主程序执行的操作的伪代码模型:
from (current directory):
for each file in this directory,
create an instance of FileInfo and load the file name and path
switch to a nested directory, or if there is none, back out of this directory
我一直在阅读有关 os.walk() 和 ok.path.walk() 的内容,但我想要一些关于在 Python 中实现此功能的最直接方法是什么的建议。提前致谢。
I'd use os.walk
执行以下操作:
def getInfos(currentDir):
infos = []
for root, dirs, files in os.walk(currentDir): # Walk directory tree
for f in files:
infos.append(FileInfo(f,root))
return infos
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)