1. 为了兼容py2,修改pathlib为os

2. 增加clean方法,清除掉创建的文件夹
This commit is contained in:
halliday 2024-02-05 11:04:56 +08:00
parent 5fd87d85c0
commit 6d31ba18c8
1 changed files with 30 additions and 29 deletions

View File

@ -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.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))
#定义公共的部分
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()