一些程序常用功能的Python实现。
创建文件夹
import os
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
print("Folder %s Created!" % (path))
else:
print("Folder %s Exists!" % (path))
复制文件
import os
import shutil
file_list = os.listdir(src_path)
file_list.sort()
for file in file_list:
src = os.path.join(src_path, file)
dst = os.path.join(dst_path, file)
shutil.copy(src, dst)
进度条
使用标准库
import sys
def status(length, percent):
sys.stdout.write('\x1B[2K') # Erase entire current line
sys.stdout.write('\x1B[0E') # Move to the beginning of the current line
progress = "Progress: ["
for i in range(0, length):
if i < length * percent:
progress += '='
else:
progress += ' '
progress += "] " + str(round(percent * 100.0, 2)) + "%"
sys.stdout.write(progress)
sys.stdout.flush()
使用tqdm库
from tqdm import tqdm
for i in tqdm(range(10000)):
...