用大模型写程序
单一的程序员以后要淘汰了。
指令:
用python写一个脚本,新建一个文件夹stock2,把文件夹stock中的文件移动到stock中,希望在mac和windows系统都能运行。详细点说明:注意stock文件夹下面还有两层,例如文件路径为stock/AAPL.US_buy/AAPL.US_2025-03-11_Buy.csv.txt,AAPL是股票名字,2025-03-11是文件日期,希望这个文件移动后到路径为stock/2025-03-11/AAPL.US/AAPL.US_2025-03-11_Buy.csv.txt
import os
import shutil
import re
from pathlib import Path
def extract_date_from_filename(filename):
# 匹配 YYYY-MM-DD 格式的日期
match = re.search(r'\d{4}-\d{2}-\d{2}', filename)
return match.group(0) if match else None
def move_files(src_root, dest_root):
src_path = Path(src_root)
if not src_path.exists():
print(f"源目录 {src_root} 不存在。")
return
for file_path in src_path.rglob('*'):
if file_path.is_file():
# 获取相对路径,例如 AAPL.US_buy/AAPL.US_2025-03-11_Buy.csv.txt
relative_path = file_path.relative_to(src_path)
parts = relative_path.parts
# 确保至少有两层目录结构
if len(parts) < 2:
continue
# 提取文件名部分用于查找日期
filename = file_path.name
date_str = extract_date_from_filename(filename)
if not date_str:
print(f"无法从文件名中提取日期: {file_path}")
continue
# 构建新的目标路径: stock2/YYYY-MM-DD/STOCK_NAME/filename
stock_name = parts[0].split('_')[0] # 从 AAPL.US_buy 提取 AAPL.US
new_dir = Path(dest_root) / date_str / stock_name
new_file_path = new_dir / filename
# 创建目标目录并移动文件
new_dir.mkdir(parents=True, exist_ok=True)
shutil.move(str(file_path), str(new_file_path))
print(f"已移动: {file_path} -> {new_file_path}")
if __name__ == '__main__':
source_folder = '../stock'
destination_folder = '../stock2'
move_files(source_folder, destination_folder)
print("所有文件已成功移动和重组。")
评论
发表评论