# 算資料夾內的資料夾及檔案總數 ```python= #算資料夾內的資料夾及檔案總數 #path1 = "C:/CompareFolder/" import os fileNum = 0 dirNum = 0 def visitDir(path): global totalSize global fileNum global dirNum for lists in os.listdir(path): sub_path = os.path.join(path, lists) #print(sub_path) if os.path.isfile(sub_path): fileNum = fileNum+1 # 統計檔案數量 elif os.path.isdir(sub_path): dirNum = dirNum+1 # 統計資料夾數量 visitDir(sub_path) # 所有子資料夾 def main(path): if not os.path.isdir(path): print('Error:"', path, '" is not a directory or does not exist.') return visitDir(path) print(path + "檔案總數:", fileNum) print(path + "資料夾總數:", dirNum) path = "C:/CompareFolder/Source_HASH" main(path) ``` ###### tags: `python` `folder` `count` `總數` `資料夾` `檔案`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up