gpt4 book ai didi

Python,os.walk(),传递信息备份?

转载 作者:行者123 更新时间:2023-11-30 23:39:10 26 4
gpt4 key购买 nike

我目前正在尝试编写一个简单的Python程序,该程序循环遍历一堆子目录,查找java文件并打印一些有关某些关键字使用次数的信息。我已经设法让这个在大部分情况下工作。我遇到的问题是打印有关更高目录的总体信息,例如,我当前的输出如下:

testcases/part1/testcase2/root_dir:
0 bytes 0 public 0 private 0 try 0 catch
testcases/part1/testcase2/root_dir/folder1:
12586 bytes 19 public 7 private 8 try 22 catch
testcases/part1/testcase2/root_dir/folder1/folder5:
7609 bytes 9 public 2 private 7 try 11 catch
testcases/part1/testcase2/root_dir/folder4:
0 bytes 0 public 0 private 0 try 0 catch
testcases/part1/testcase2/root_dir/folder4/folder2:
7211 bytes 9 public 2 private 4 try 9 catch
testcases/part1/testcase2/root_dir/folder4/folder3:
0 bytes 0 public 0 private 0 try 0 catch

我希望输出是:

testcases/part1/testcase2/root_dir :
27406 bytes 37 public 11 private 19 try 42 catch
testcases/part1/testcase2/root_dir/folder1 :
20195 bytes 28 public 9 private 15 try 33 catch
testcases/part1/testcase2/root_dir/folder1/folder5 :
7609 bytes 9 public 2 private 7 try 11 catch
testcases/part1/testcase2/root_dir/folder4 :
7211 bytes 9 public 2 private 4 try 9 catch
testcases/part1/testcase2/root_dir/folder4/folder2 :
7211 bytes 9 public 2 private 4 try 9 catch
testcases/part1/testcase2/root_dir/folder4/folder3 :
0 bytes 0 public 0 private 0 try 0 catch

正如您所看到的,较低的子目录直接向较高的子目录提供信息。这是我遇到的问题。如何有效地实现这一点。我考虑过将每个打印存储为列表中的字符串,然后在最后打印所有内容,但我认为这不适用于多个子目录,例如提供的示例。这是我到目前为止的代码:

def lsJava(path):

print()

for dirname, dirnames, filenames in os.walk(path):

size = 0
public = 0
private = 0
tryCount = 0
catch = 0

#Get stats by current directory.
tempStats = os.stat(dirname)

#Print current directory information

print(dirname + ":")

#Print files of directory.
for filename in filenames:
if(filename.endswith(".java")):
fileTempStats = os.stat(dirname + "/" + filename)
size += fileTempStats[6]
tempFile = open(dirname + "/" + filename)
tempString = tempFile.read()
tempString = removeComments(tempString)
public += tempString.count("public", 0, len(tempString))
private += tempString.count("private", 0, len(tempString))
tryCount += tempString.count("try", 0, len(tempString))
catch += tempString.count("catch", 0, len(tempString))

print(" ", size, " bytes ", public, " public ",
private, " private ", tryCount, " try ", catch,
" catch")

removeComments 函数只是使用正则表达式模式从 java 文件中删除所有注释。感谢您提前提供的任何帮助。

编辑:

在 for 循环的开头添加以下代码:

    current_dirpath = dirname

if( dirname != current_dirpath):
size = 0
public = 0
private = 0
tryCount = 0
catch = 0

现在的输出如下:

testcases/part1/testcase2/root_dir/folder1/folder5:
7609 bytes 9 public 2 private 7 try 11 catch
testcases/part1/testcase2/root_dir/folder1:
20195 bytes 28 public 9 private 15 try 33 catch
testcases/part1/testcase2/root_dir/folder4/folder2:
27406 bytes 37 public 11 private 19 try 42 catch
testcases/part1/testcase2/root_dir/folder4/folder3:
27406 bytes 37 public 11 private 19 try 42 catch
testcases/part1/testcase2/root_dir/folder4:
27406 bytes 37 public 11 private 19 try 42 catch
testcases/part1/testcase2/root_dir:
27406 bytes 37 public 11 private 19 try 42 catch

最佳答案

os.walk() 采用可选的 topdown 参数。如果您使用 os.walk(path, topdown=False) ,它将改为自下而上遍历目录。

当您第一次启动循环时,将元组的第一个元素(dirpath)保存为变量,例如current_dirpath。当您继续循环时,您可以保留该目录中文件大小的运行总数。然后只需添加一个像 if dirpath != current_dirpath 这样的检查,此时您就知道您已经上升到目录级别,并且可以重置总数。

关于Python,os.walk(),传递信息备份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com