优化方法

This commit is contained in:
halliday 2025-11-18 18:55:06 +08:00
parent c9605ad3b5
commit d2c505fd07
1 changed files with 74 additions and 68 deletions

View File

@ -1,74 +1,80 @@
#encoding=utf-8
import shutil
import sys
import os
from pathlib import Path
"""
69-477326 :: 版本: 1 :: 文件内部处理频繁切换防止溢出测试
使用方法
1.先用WriteFile 2.0.exe 工具生成一个 test.txt
2.和这个脚本放在一个目录中
3.执行脚本
python copy_files.py args
args:
init # 初始化目录会创建60个目录,有的会先删除
clean # 清楚创建的目录
js # 生成 test_1.txttest_3.txt ...,并依次放入上述目录中
os # 生成 test_2.txttest_4.txt ...,并依次放入上述目录中
"""
# 定义公共的部分
path = os.getcwd()
file_path = os.path.join(path, 'test.txt')
dir_list = list(range(1, 61))
# 复制奇数
def jishu():
file_list = list(range(121))[1::2]
fd = dict(zip(file_list, dir_list))
copy_file(fd)
# 复制偶数
def oushu():
file_list = list(range(121))[2::2]
fd = dict(zip(file_list, dir_list))
copy_file(fd)
# 复制文件到目录中
def copy_file(fd):
for file, dir in fd.items():
newname = 'test_' + str(file) + '.txt'
todir = os.path.join(path, str(dir), newname)
shutil.copyfile(file_path, todir)
# 初始化目录
def init_dir():
for dir in dir_list:
dir_path = os.path.join(path, str(dir))
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
os.mkdir(dir_path)
def clean():
for dir in dir_list:
dir_path = os.path.join(path, str(dir))
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
class FileCopier:
def __init__(self):
self.path = Path.cwd()
self.file_path = self.path / 'file.txt'
self.dir_list = list(range(1, 61))
def init_dir(self):
"""初始化目录"""
for dir_num in self.dir_list:
dir_path = self.path / str(dir_num)
if dir_path.exists():
shutil.rmtree(dir_path)
dir_path.mkdir()
print("目录初始化完成")
def _copy_files(self, file_list):
"""复制文件到目录中"""
if not self.file_path.exists():
raise FileNotFoundError(f"源文件 {self.file_path} 不存在")
file_dir_map = dict(zip(file_list, self.dir_list))
success_count = 0
for file_num, dir_num in file_dir_map.items():
new_name = f'file_{file_num}.txt'
target_path = self.path / str(dir_num) / new_name
try:
shutil.copyfile(self.file_path, target_path)
success_count += 1
except Exception as e:
print(f"复制文件 {file_num} 到目录 {dir_num} 失败: {e}")
print(f"成功复制 {success_count} 个文件")
def copy_odd(self):
"""复制奇数文件"""
file_list = list(range(1, 121, 2))
print(f"开始复制 {len(file_list)} 个奇数文件...")
self._copy_files(file_list)
def copy_even(self):
"""复制偶数文件"""
file_list = list(range(2, 121, 2))
print(f"开始复制 {len(file_list)} 个偶数文件...")
self._copy_files(file_list)
def main():
"""主函数"""
if len(sys.argv) != 2:
print("用法: python copy_files.py [init|js|os]")
print(" init - 初始化目录")
print(" js - 复制奇数文件")
print(" os - 复制偶数文件")
sys.exit(1)
command = sys.argv[1].lower()
copier = FileCopier()
try:
if command == 'init':
copier.init_dir()
elif command == 'js':
copier.copy_odd()
elif command == 'os':
copier.copy_even()
else:
print(f"错误: 未知命令 '{command}'")
print("请使用 init, js 或 os")
sys.exit(1)
except Exception as e:
print(f"执行失败: {e}")
sys.exit(1)
if __name__ == '__main__':
# 定义传参
if len(sys.argv) != 2:
assert False, 'not get args'
if sys.argv[1] not in ["js", "os", "init","clean"]:
assert False, 'args is error,please input js or os '
if sys.argv[1] == 'init':
init_dir()
elif sys.argv[1] == 'js':
jishu()
elif sys.argv[1] == 'os':
oushu()
elif sys.argv[1] == 'clean':
clean()
main()