OS模块
# 返回当前目录
os.getcwd()
# 返回文件名
os.path.basename(path)
# 返回文件路径
os.path.dirname(path)
字节换算
def bytes_conversion(number):
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
prefix = dict()
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if int(number) >= prefix[s]:
value = float(number) / prefix[s]
return '%.2f%s' % (value, s)
return "%sB" % number
随机密码生成
import random
import string
a=string.ascii_letters+string.digits
key=[]
def getKey():
key=random.sample(a,8)
keys="".join(key)
return keys
for i in range(1000):
print(getKey())
用Windows文件资源管理器打开文件并选中
import os, pathlib
file = r'D:/Users/Haujet/Desktop/1.png'
os.system(f'explorer /select, "{pathlib.Path(file)}')