From 6d31ba18c80b4dad3aa52211237ef9c262d7e0e2 Mon Sep 17 00:00:00 2001 From: halliday Date: Mon, 5 Feb 2024 11:04:56 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=B8=BA=E4=BA=86=E5=85=BC=E5=AE=B9py2,?= =?UTF-8?q?=E4=BF=AE=E6=94=B9pathlib=E4=B8=BAos=202.=20=E5=A2=9E=E5=8A=A0c?= =?UTF-8?q?lean=E6=96=B9=E6=B3=95=EF=BC=8C=E6=B8=85=E9=99=A4=E6=8E=89?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{copy_files.py => 69-477326.py} | 59 ++++++++++--------- 1 file changed, 30 insertions(+), 29 deletions(-) rename 复制奇偶数文件的脚本/{copy_files.py => 69-477326.py} (52%) diff --git a/复制奇偶数文件的脚本/copy_files.py b/复制奇偶数文件的脚本/69-477326.py similarity index 52% rename from 复制奇偶数文件的脚本/copy_files.py rename to 复制奇偶数文件的脚本/69-477326.py index 1c56851..2445f0b 100644 --- a/复制奇偶数文件的脚本/copy_files.py +++ b/复制奇偶数文件的脚本/69-477326.py @@ -1,8 +1,6 @@ -#encoding=utf-8 import shutil import sys - -from pathlib import Path +import os """ 69-477326 :: 版本: 1 :: 文件内部处理频繁切换,防止溢出测试 @@ -14,55 +12,57 @@ from pathlib import Path python copy_files.py args args: init # 初始化目录,会创建60个目录,有的会先删除 + clean # 清楚创建的目录 js # 生成 test_1.txt,test_3.txt ...,并依次放入上述目录中 os # 生成 test_2.txt,test_4.txt ...,并依次放入上述目录中 """ +# 定义公共的部分 +path = os.getcwd() +file_path = os.path.join(path, 'test.txt') +dir_list = list(range(1, 61)) - - -#定义公共的部分 -path = Path.cwd() -file_path = 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)) + 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)) + fd = dict(zip(file_list, dir_list)) copy_file(fd) -#复制文件到目录中 +# 复制文件到目录中 def copy_file(fd): - for file,dir in fd.items(): + for file, dir in fd.items(): newname = 'test_' + str(file) + '.txt' - todir = path / str(dir) / newname + todir = os.path.join(path, str(dir), newname) shutil.copyfile(file_path, todir) -#初始化目录 +# 初始化目录 def init_dir(): for dir in dir_list: - dir_path = path / str(dir) - if dir_path.exists(): - shutil.rmtree(dir_path) - dir_path.mkdir() + 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) if __name__ == '__main__': - -#定义传参 + # 定义传参 if len(sys.argv) != 2: - assert False,'not get args' - - if sys.argv[1] not in ["js","os","init"]: - assert False,'args is error,please input js or os ' + 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() @@ -70,4 +70,5 @@ if __name__ == '__main__': jishu() elif sys.argv[1] == 'os': oushu() - + elif sys.argv[1] == 'clean': + clean()