1, retrieve folder size
Required to perform as follows:
python3.8 run.py folder
import os import sys def file_size(file_path): size = 0 if not os.path.isdir(file_path): return file_list = os.listdir(file_path) for name in file_list: son_path = os.path.join(file_path, name) if os.path.isdir(son_path): size += file_size(son_path) # elif os.path.isfile(son_path): else: size += os.path.getsize(son_path) return size file_path = sys.argv[1] size_of_file = file_size(file_path) print(size_of_file)
2, the random codes
Import Random # random codes DEF random_code (= size. 4 ): code = "" for I in Range (size): alp_up = CHR (the random.randint (65, 90, )) alp_low = CHR (the random.randint (97,122 )) NUM = STR (the random.randint (0,9 )) # the second list weights = [] stored in the weight of each element, k is the selected frequency value is returned list single_code = random.choices ([alp_low, alp_up , num] , weights = [1,1,2], K = 2 ) code + = single_code [0] return code Print (random_code ())
3, analog download progress bar
Import Time DEF downloads (): Total = 50000 Speed = 1024 acum = 0 the while acum < Total: the time.sleep ( 0.1 ) acum + = Speed Percent = acum / Total IF Percent>. 1 : Percent =. 1 Wide = int (* Percent 50 ) show_str = " # " * Wide # every time the print result before covering, dynamic effects. percent * 100 percent loaded display print("\r[%-50s] %d%%" %(show_str,percent*100),end="") download()
4, text copy script
# 法一: import sys src_file = sys.argv[1] dst_file = sys.argv[2] with open(src_file,mode="rb") as f,\ open(dst_file,mode="wb") as f1: f1.write(f.read()) #==================================== # 法二: import sys import shutile src_file = sys.argv[1] dst_file = sys.argv[2] shutil.copyfile(src_file, dst_file)