Python 代码:自动创建文件夹并保存数据到文件
import os
def save_to_file(data, file_path): '判断保存路径是否存在,不存在则创建' if not os.path.exists(os.path.dirname(file_path)): os.makedirs(os.path.dirname(file_path)) with open(file_path, 'w') as f: f.write(data)
data = 'hello world' file_path = 'test/hello.txt' save_to_file(data, file_path)
原文地址: https://www.cveoy.top/t/topic/m032 著作权归作者所有。请勿转载和采集!