当前位置: 首页 > news >正文

一个典型的要求: Python | C#实现年月日创建文件夹 时分秒对应文件名的保存路径

赶时间先说python: 用年月日作为文件夹:

年月日 时分秒 保存文件的路径根据年月日 创建文件夹 年里面包含月 月里面包含日 检查是否存在 没有就去创建 最后文件名用 时分秒毫秒.txt

这是一个典型的要求:

import os
from datetime import datetimenow = datetime.now()# 获取当前时间# 提取年、月、日、时、分、秒、毫秒
year        = str(now.year)
month       = str(now.month).zfill(2)  # 补零,保证两位数
day         = str(now.day).zfill(2)
hour        = str(now.hour).zfill(2)
minute      = str(now.minute).zfill(2)
second      = str(now.second).zfill(2)
millisecond = str(now.microsecond // 1000).zfill(4)  # 毫秒# 构建文件夹路径
#folder_path = os.path.join(year, month, day)# 年月日
#folder_path = os.path.join(year, month, day,hour,minute)#年月日时分
folder_path = os.path.join(year, month, day,hour)#年月日时# 检查并创建文件夹
if not os.path.exists(folder_path):os.makedirs(folder_path)#递归创建文件夹# 构建文件名
file_name = f"{hour}_{minute}_{second}_{millisecond}.txt"  # 使用下划线分隔,避免冒号问题
file_path = os.path.join(folder_path, file_name)# 写入内容到文件
with open(file_path, "w") as file:file.write(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S.%f')}")print(f"文件已创建: {file_path}")
input()

当然还可以根据需要 修改 假设你的项目是以多个产品为主导 那么就需要根据产品名去归属文件.

先来C#写完在说别的.

        static void Run(){//using System;//using System.IO;// 获取当前时间DateTime now = DateTime.Now;// 提取年、月、日、时、分、秒、毫秒string year = now.Year.ToString();string month = now.Month.ToString("D2"); // 补零,保证两位数string day = now.Day.ToString("D2");string hour = now.Hour.ToString("D2");string minute = now.Minute.ToString("D2");string second = now.Second.ToString("D2");string millisecond = now.Millisecond.ToString("D6"); // 毫秒// 构建文件夹路径string folderPath = Path.Combine(year, month, day);// 检查并创建文件夹if (!Directory.Exists(folderPath)){Directory.CreateDirectory(folderPath);}// 构建文件名string fileName = $"{hour}_{minute}_{second}_{millisecond}.txt"; // 使用下划线分隔,避免冒号问题string filePath = Path.Combine(folderPath, fileName);// 写入内容到文件File.WriteAllText(filePath, $"当前时间: {now:yyyy-MM-dd HH:mm:ss.fff}");Console.WriteLine($"文件已创建: {filePath}");}

和pyhon几乎没区别 

假设你的产品是


string[] fruits = new string[] { "香蕉", "苹果", "橘子", "橙子", "栗子", "梨子", "西瓜", "菠萝", "葡萄", "樱桃", "草莓", "桃子", "杏子", "芒果", "猕猴桃" };

那么:

        static void Run00(){// 获取当前工作目录string currentDirectory = Environment.CurrentDirectory;// 构建目标文件夹路径string SaveImg = "SaveImg"; // 目标文件夹名称// 构建水果数组string[] fruits = new string[]{ "香蕉", "苹果", "橘子", "橙子", "栗子", "梨子", "西瓜", "菠萝", "葡萄", "樱桃", "草莓", "桃子", "杏子", "芒果", "猕猴桃 "};DateTime now = DateTime.Now;// 提取年、月、日、时、分、秒、毫秒string year = now.Year.ToString();string month = now.Month.ToString("D2"); // 补零,保证两位数string day = now.Day.ToString("D2");string hour = now.Hour.ToString("D2");string minute = now.Minute.ToString("D2");string second = now.Second.ToString("D2");string millisecond = now.Millisecond.ToString("D6"); // 毫秒string folderPath = Path.Combine(year, month, day);// 选择第一个水果作为文件夹名称int i = 0;// 构建完整路径string x = Path.Combine(currentDirectory, SaveImg, fruits[i], folderPath);// 检查并创建文件夹if (!Directory.Exists(x)){Directory.CreateDirectory(x);}// 构建文件名string fileName = $"{hour}_{minute}_{second}_{millisecond}.txt"; // 使用下划线分隔,避免冒号问题string filePath = Path.Combine(x, fileName);// 写入内容到文件File.WriteAllText(filePath, $"当前时间: {now:yyyy-MM-dd HH:mm:ss.fff}");// 输出文件路径和当前工作目录Console.WriteLine($"文件已创建: {filePath}");Console.WriteLine("当前工作目录: " + currentDirectory);}

好了现在可以在程序运行同级目录下,"SaveImg"文件夹下的 :产品名称:"香蕉":里面根据 年月日创建文件夹.并且在最终目录生成一个时分秒毫秒命名的文件 ;

http://www.lryc.cn/news/543758.html

相关文章:

  • 知识库功能测试难点
  • 如何实现某短视频平台批量作品ID的作品详情采集
  • uniapp中使用leaferui使用Canvas绘制复杂异形表格的实现方法
  • 判别分析:原理推导、方法对比与Matlab实战
  • PMP项目管理—整合管理篇—4.管理项目知识
  • Makefile编写和相关语法规则
  • 点云 PCL 滤波在自动驾驶的用途。
  • NL2SQL的应用-长上下文模型在处理NL2SQL任务时,相较于传统模型,有哪些显著的优势
  • 图像处理基础(8):图像的灰度直方图、直方图均衡化、直方图规定化(匹配)
  • 探寻数组中两个不重复数字的奥秘:C 语言实战之旅
  • Mercury、LLaDA 扩散大语言模型
  • 【ESP32S3接入讯飞在线语音识别】
  • 深入了解 SSH 及其相关协议
  • 微信小程序源码逆向 MacOS
  • 【我的 PWN 学习手札】House of Husk
  • (八)趣学设计模式 之 装饰器模式!
  • 设计后端返回给前端的返回体
  • Element Plus中el-select选择器的下拉选项列表的样式设置
  • C高级(shell)
  • 子宫腺肌症是如果引起的?
  • 网络安全学习中,web渗透的测试流程是怎样的?
  • 【软考】【2025年系统分析师拿证之路】【啃书】第十四章 软件实现与测试(十五)
  • 自然语言处理NLP深探
  • 加载互联网免费地图资源并通过CesiumEarth快速浏览
  • Android 键盘输入按确认或换行 直接触发提交
  • halcon三维点云数据处理(二十七)remove_bin_for_3d_object_localization
  • XFeat:轻量级的深度学习图像特征匹配
  • [MD] AG stable
  • 微信小程序自定义导航栏实现指南
  • wav格式的音频压缩,WAV 转 MP3 VBR 体积缩减比为 13.5%、多个 MP3 格式音频合并为一个、文件夹存在则删除重建,不存在则直接建立