# 当前python的同级目录下有一个utils.py文件,文件中定义了一个load_models函数 from utils import load_models
读压缩包
python可以在不解压文件的情况下读取文件内容
import zipfile # zip with zipfile.ZipFile(f'{file_name}.zip', 'r') as zip_ref: for name in zip_ref.namelist(): print(name) content = zip_ref.read(name).decode('utf-8')
import tarfile # tar with tarfile.open(f'uploads/{file_name}.tar', 'r') as tar_ref: for member in tar_ref.getmembers(): print(member.name) content = tar_ref.extractfile(member).read().decode('utf-8')
defread_pkl_file(file_path): withopen(file_path, 'rb') as file: data = pickle.load(file) return data defwrite_pkl_file(file_path, data): withopen(file_path, 'wb') as file: pickle.dump(data, file)
随机数
ans = random.uniform(c_min, c_max)
json
# 读字符串为json data = json.loads(input_text)
moviepy
moviepy是一个处理视频和音频的库
为视频配音
video = mp.VideoFileClip("1.mp4") audio = mp.AudioFileClip("2.mp3") video = video.set_audio(audio) video.write_videofile("3.mp4", codec="libx264", audio_codec="aac")