簡體   English   中英

如何計算給定目錄中文件的總行數? 在python中

[英]How do I count the total number of lines of files in a given directory? in python

如何使用Python計算給定目錄中文件的總行數?

用偽代碼(不可執行):

total_length = 0
file_list = fetch_file_list() 
# USE: os.walk or glob.glob depending on whether you want to filter the files.
for file_path in file_list:
    file_path = make_path_absolute(file_path)
    # This can be as simple as os.path.join(your_root_path, file_path)
    file_object = open(file_path) 
    # Don't forget there are different modes -- will all the files be text?
    total_length += length(file_object.readlines()) 
    # YOU MIGHT WANT TO CONSIDER: What if some of the files are large?
print "There are", length(file_list), "files in", \
    file_path, "containing", total_length, "lines of text."

查看Python操作系統以獲取文件列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM