python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計
更新時間:2019年02月25日 10:20:09 作者:fengbingchun
這篇文章主要為大家詳細介紹了python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下
有時會統(tǒng)計某個目錄下有哪些文件,每個文件的sha256及文件大小等相關信息,這里用python3寫了個腳本用來實現(xiàn)此功能,此腳本可跨平臺,同時支持windows和linux,腳本(get_dir_file_info.py)內(nèi)容如下:
import os
import sys
import hashlib
def Usage():
''' usage description '''
num = len(sys.argv)
if num != 3:
print("Error: please input two parameters")
print("for example: {} path_name save_file_name".format(sys.argv[0]))
sys.exit(1)
def GetFilesList():
''' get file list '''
input_path_name = sys.argv[1]
result = list()
for dirpath, dirnames, filenames in os.walk(input_path_name, followlinks=True):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
result.append(file_path)
result.sort()
return result
def CalcFileSha256(filname):
''' calculate file sha256 '''
with open(filname, "rb") as f:
sha256obj = hashlib.sha256()
sha256obj.update(f.read())
hash_value = sha256obj.hexdigest()
return hash_value
def CalcFileSize(filename):
''' calculate file size '''
return os.stat(filename).st_size
def GetFileContent():
''' get file contnet '''
files_list = GetFilesList()
result = list()
for f in files_list:
hash = CalcFileSha256(f)
size = CalcFileSize(f)
file_name = os.path.basename(os.path.realpath(f))
path_name = os.path.dirname(os.path.realpath(f))
dictionary = {"path": path_name, "filename": file_name, "sha256": hash, "size": size}
#print("result: {}".format(dictionary))
result.append(dictionary)
return result
def WriteToFile(contents):
''' write content to the specified file '''
fp = open(sys.argv[2], "w")
for content in contents:
#print("content:", content)
str0 = str(content)
str1 = str0.replace("\\\\", "/")
fp.write(str1)
fp.write("\n")
fp.close()
def ReplaceStr(src_str, new_str):
''' replace source string with new string '''
contents = list()
fp = open(sys.argv[2], "r")
line = fp.readline()
while line:
contents.append(line)
line = fp.readline()
fp.close()
fp = open(sys.argv[2], "w")
for content in contents:
str0 = content.replace(src_str, new_str)
fp.write(str0)
fp.close()
def main():
Usage()
WriteToFile(GetFileContent())
ReplaceStr(" ", "")
ReplaceStr("'", "\"")
if __name__ == "__main__":
main()
執(zhí)行操作如下:

執(zhí)行結果及生成的prj_file_list.txt內(nèi)容如下:

GitHub:Python_Test
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python讀取系統(tǒng)文件夾內(nèi)所有文件并統(tǒng)計數(shù)量的方法
- Python統(tǒng)計純文本文件中英文單詞出現(xiàn)個數(shù)的方法總結【測試可用】
- Python實現(xiàn)統(tǒng)計文本文件字數(shù)的方法
- Python統(tǒng)計文件中去重后uuid個數(shù)的方法
- Python實現(xiàn)對excel文件列表值進行統(tǒng)計的方法
- python統(tǒng)計文本文件內(nèi)單詞數(shù)量的方法
- Python3讀取UTF-8文件及統(tǒng)計文件行數(shù)的方法
- python 遠程統(tǒng)計文件代碼分享
- python腳本實現(xiàn)統(tǒng)計日志文件中的ip訪問次數(shù)代碼分享
- 使用python統(tǒng)計文件行數(shù)示例分享
相關文章
pytorch神經(jīng)網(wǎng)絡之卷積層與全連接層參數(shù)的設置方法
今天小編就為大家分享一篇pytorch神經(jīng)網(wǎng)絡之卷積層與全連接層參數(shù)的設置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python數(shù)字圖像處理之基本形態(tài)學濾波
這篇文章主要為大家介紹了python數(shù)字圖像處理之基本形態(tài)學濾波示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
如何實現(xiàn)在pycharm中將.ui文件轉化為.py文件
這篇文章主要介紹了如何實現(xiàn)在pycharm中將.ui文件轉化為.py文件,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06

