倘若想记录代码运行过程中的结果文件,那么以下函数仅供参考
代码示例:
import os
import datetime
import sys
import pandas as pd
# 定义总的文件夹路径
base_folder = 'E:\\D\\log\\product_data_compare_log'
# 定义一个函数来创建带时间戳的文件夹
def create_timestamped_folder(base_folder):
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
folder_name = os.path.join(base_folder, timestamp)
os.makedirs(folder_name, exist_ok=True)
return folder_name
# 定义一个函数来保存文件,文件名包含时间戳
def save_file_with_timestamp(folder_path, filename_prefix, content, file_extension):
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f"{filename_prefix}_{timestamp}.{file_extension}"
file_path = os.path.join(folder_path, filename)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
return file_path
# 方法1: 抓取网站的原始html
def fetch_website_html():
# 这里添加获取html的代码,例如使用requests库
# ...
html_content = "<!DOCTYPE html><html><body>Website content...</body></html>"
return html_content
# 运行代码
if __name__ == '__main__':
# 创建带时间戳的文件夹
folder_path = create_timestamped_folder(base_folder)
print(f"Created folder: {folder_path}")
# 方法1: 抓取网站的原始html并保存
html_content = fetch_website_html()
html_file_path = save_file_with_timestamp(folder_path, 'website_html', html_content, 'html')
print(f"Saved html to: {html_file_path}")
运行结果;
以上,即可生成带时间戳的文件夹,记录每次的结果文件。