以下のように書くと、よさそう
#!/usr/local/bin/python # -*- coding: utf-8 -*- import datetime import os import sys import platform from smb.SMBConnection import SMBConnection import time conf = {"user_id":"ないしょ", "user_pw":"ないしょ", "remote_netbios":'CBBないしょ4252', "remote_ip":"ないしょ", # "remote_port":139, "remote_port":445, # tera station?に対しては "is_direct_tcp": True, #ここがTrue & port445がよさそう "remote_path":"ファイルサーバー", "sleep": 0.2} def main(): smb_conn = SMBConnection(conf["user_id"], conf["user_pw"], platform.uname().node, # client netbios conf["remote_netbios"], is_direct_tcp=conf["is_direct_tcp"]) try: smb_conn.connect(conf["remote_ip"],conf["remote_port"]) except Exception as e: print('Connect failed. Reason: %s', e) return False start_dir = "" if len(sys.argv) == 2: start_dir = sys.argv[1] else: print("USAGE:",sys.argv[0],"start_dir") return None files_summary = {} aggregate_dir( smb_conn, start_dir, files_summary) smb_conn.close() sorted_summary = sorted(files_summary.items(), key=lambda x:x[0]) for summary in sorted_summary: yyyy,file_type = summary[0].split("\t") disp_str = "\t".join([str(yyyy), file_type, str(summary[1]["byte"]), str(summary[1]["count"]) ]) print( disp_str) def aggregate_dir( smb_conn, org_dir, files_summary ): print("aggregating ...", org_dir) time.sleep(conf["sleep"]) dir_or_files = smb_conn.listPath(conf["remote_path"],org_dir) for dir_or_file in dir_or_files: if dir_or_file.filename=="." or dir_or_file.filename=="..": continue if dir_or_file.isDirectory: next_dir = org_dir +"/"+ dir_or_file.filename aggregate_dir( smb_conn, next_dir, files_summary) continue last_unix_time = dir_or_file.last_write_time if last_unix_time < dir_or_file.last_access_time: last_unix_time = dir_or_file.last_access_time atime = date_str_from_unixtime(last_unix_time) file_ext = dir_or_file.filename.split(".")[-1].upper() summary_key = atime +"\t"+file_ext if (summary_key in files_summary) == False: files_summary[summary_key] = {"byte":0, "count":0} files_summary[summary_key]["count"] += 1 files_summary[summary_key]["byte"] += dir_or_file.file_size def date_str_from_unixtime(org_val): dt = datetime.datetime.fromtimestamp(org_val) # print(dt.strftime('%Y-%m-%d %H:%M:%S')) return dt.strftime('%Y') if __name__ == '__main__': main()