赶时间先说python: 用年月日作为文件夹:
年月日 时分秒 保存文件的路径根据年月日 创建文件夹 年里面包含月 月里面包含日 检查是否存在 没有就去创建 最后文件名用 时分秒毫秒.txt
这是一个典型的要求:
import os
from datetime import datetime
now = 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"文件夹下的 :产品名称:"香蕉":里面根据 年月日创建文件夹.并且在最终目录生成一个时分秒毫秒命名的文件 ;