Python move “met conditional value” outside of loop
这可能会令人困惑。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | rootdir= C:\User\Desktop\File file = 'file.txt' mainLocNum = str(list(rootdir)).count(r'\') mainFolder=os.listdir(rootdir) with open(file,'w') as f: for dir, subdirs, files in os.walk(rootdir): currentDirLevel=str(list(dir)).count(r'\') for allFolders in subdirs: if (currentDirLevel - mainLocNum) == 0: parentFolders=allFolders f.write(str(parentFolders)) PLACEHOLDER elif (currentDirLevel - mainLocNum) == 1: subFolders=allFolders f.write(str(allFolders) <----- write this in PLACEHOLDER |
我只想在满足
我一直在尝试不同的嵌套方法,但我缺乏基本的循环构造逻辑。
感谢您的帮助,谢谢!
编辑:
我正在循环访问一个主目录,并将所有父文件夹写入文本文件。我想在每个父文件夹及其子文件夹之间写入:即,如果父文件夹包含更多文件夹,则在每个父文件夹之间写入这些文件夹;如果父文件夹不包含任何其他文件夹,则跳到下一个父文件夹等。我使用if(currentdirlevel-mainlocnum)==(number)来知道有多少个目录它正在进入并为每个步骤执行不同的写入函数。
我正在尝试根据文件夹是否是一级子目录、二级子目录等以特定格式写入文件夹的名称…
我想要什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ParentFolder1 SubFolder1 Subfolder2 SubSubFolder1 SubFolder3 ParentFolder2 SubFolder1 ParentFolder3 ParentFolder4 SubFolder1 SubSubFolder1 SubSubSubFolder1 SubSubFolder2 SubFolder2 ParentFolder5 SubFolder1 |
我得到了什么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ParentFolder1 ParentFolder2 ParentFolder3 ParentFolder4 ParentFolder5 SubFolder1 SubFolder2 SubFolder3 SubFolder1 SubFolder1 SubFolder2 SubFolder1 SubSubFolder1 SubSubFolder1 SubSubFolder2 SubSubSubFolder1 |
请不要关注os.walk或遍历目录。我已经编写了很多代码,我希望主焦点能够回答我关于运行条件循环并将该循环中的值放入另一个循环中的写入函数的问题。
我希望重新构造这个循环逻辑,而不是从整个os.walk for loop重新开始。
再次感谢
我不太清楚你所说的"条件循环"是什么意思,但是你想实现的是使用一个基于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import os tab = 4 * ' ' def writedirs(fhandle, path, depth=0): ''' Recursively walk the directory tree starting at path, writing all directory names to open file handle fhandle. Nodes are traversed depth-first, top-down, and names are indented proportional to their depth. ''' data = os.listdir(path) # Names returned by listdir are in file system order; # If you want them sorted alphabetically, call # data.sort() # or # data.sort(key=str.lower) # for case-insensitive sorting. indent = depth * tab depth += 1 for filename in data: fullpath = os.path.join(path, filename) if os.path.isdir(fullpath): fhandle.write(indent + filename + ' ') writedirs(fhandle, fullpath, depth) #Test rootdir = 'testfolder' outname = 'file.txt' with open(outname, 'w') as fhandle: writedirs(fhandle, rootdir) |
"file.txt"的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ParentFolder1 SubFolder1 Subfolder2 SubSubFolder1 SubFolder3 ParentFolder2 SubFolder1 ParentFolder3 ParentFolder4 SubFolder1 SubSubFolder1 SubSubSubFolder1 SubSubFolder2 SubFolder2 ParentFolder5 SubFolder1 |
在实际情况下,最好避免在python中递归:python解释器不能执行尾调用消除,并且它施加了最大的递归深度。但是,在处理递归数据结构(如文件树)时,使用递归算法是很自然的。
fwiw,下面的代码迭代地执行上面代码的逆操作;我使用它从问题中给出的目录名缩进列表中构建目录树。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import os data = ''' ParentFolder1 SubFolder1 Subfolder2 SubSubFolder1 SubFolder3 ParentFolder2 SubFolder1 ParentFolder3 ParentFolder4 SubFolder1 SubSubFolder1 SubSubSubFolder1 SubSubFolder2 SubFolder2 ParentFolder5 SubFolder1 '''[1:] def show(seq): for row in seq: print(row) print() def stripcount(s): z = s.lstrip(' ') count = len(s) - len(z) return z, count joinpath = os.path.join def make_dir_tree(dir_tree, root=''): ''' Create a directory tree in root from dir_tree, which is a list of indented directory names. ''' dir_tree = [stripcount(s) for s in dir_tree] #show(dir_tree) stack = [root] depth = -1 for dname, count in dir_tree: if count > depth: depth = count stack.append(dname) elif count < depth: depth = count stack.pop() stack[-1] = dname else: stack[-1] = dname pathname = joinpath(*stack) print(pathname) os.mkdir(pathname) dir_tree = data.splitlines() show(dir_tree) make_dir_tree(dir_tree, 'testfolder') |