[Python] 纯文本查看 复制代码 # Time: 2019/10/5 15:27
import os
import shutil
def copy_file(source_path, target_path):
try:
os.mkdir(target_path)
except FileExistsError:
print('目标文件夹已存在')
count = 1
for i in os.walk(source_path):
if 'Video_Export' in i[0]:
video_path = os.path.join(i[0], i[2][0])
shutil.copy(video_path, target_path)
print('成功复制第【%d】视频' % count)
count += 1
print('文件复制完成')
def move_file(source_path, target_path):
try:
os.mkdir(target_path)
except FileExistsError:
print('目标文件夹已存在')
count = 1
for i in os.walk(source_path):
if 'Video_Export' in i[0]:
video_path = os.path.join(i[0], i[2][0])
shutil.move(video_path, target_path)
print('成功移动第【%d】视频' % count)
count += 1
print('文件移动完成')
def move_file_one(source_path):
count = 1
for i in os.walk(source_path):
# print(start)
try:
if '新建文件夹' == os.path.split(i[0])[1]:
shutil.rmtree(i[0])
continue
if 'Video_Export' in i[1][0]:
for child in i[2]:
del_file = os.path.join(i[0], child)
os.remove(del_file)
except IndexError:
pass
print('删除文件(夹)完成')
for i in os.walk(source_path):
if 'Video_Export' in i[0]:
video_path = os.path.join(i[0], i[2][0])
target_path = os.path.split(i[0])[0]
# print(target_path)
shutil.move(video_path, target_path)
print('成功移动第【%d】视频' % count)
count += 1
for i in os.walk(source_path):
if 'Video_Export' in i[0]:
# print(start[0])
shutil.rmtree(i[0])
print('*****文件操作完成*****')
if __name__ == '__main__':
# source_path = r"D:\各种文件\Actime\2020-1-22\复制\建材化学分析李宛余已翻11文本加视频\6 18李宛余已翻11"
# target_path = r"D:\各种文件\Actime\2020-1-22\复制\建材化学分析李宛余已翻11文本加视频"
# move_file(source_path, target_path)
# # copy_file(source_path, target_path)
source_path = r"D:\Impurity\Actime\12-1\12-1已完成"
move_file_one(source_path) # 移动到上一层目录
|