import os
import shutil
def separate(src_path, target_path):
files = os.listdir(src_path)
s = []
for file in files: # 遍历文件夹
if not os.path.isdir(file): # 判断是否是文件夹,不是文件夹才打开
#f = open(src_path + "/" + file); # 打开文件
# #iter_f = iter(f); # 创建迭代器
# str = ""
# for line in iter_f: # 遍历文件,一行行遍历,读取文本
# str = str + line
if file[-3:]=='jpg':
str = file
s.append(str) # 每个文件的文本存到list中
print(s) # 打印结果
print(len(s))
for file in files:
if file[-2:]=='pt':
str = file[:-2]+'jpg'
s.remove(str)
#shutil.move(src_path+'/'+str, target_path + '/' + str)
for ele in s:
shutil.move(src_path + '/' + ele, target_path + '/' + ele)
print(s)
print(len(s))
return
if __name__ == "__main__":
src_path = './TEST/source1_100'
suffix = '.jpg .jpeg .png'
#outfile = "./byf/list_dir.txt"
#file = open(outfile,'w')
target_path = './TEST/test_s100'
separate(src_path,target_path)
#file.close()
Python 拆分文件夹中的文件 进行转移
最新推荐文章于 2023-08-14 14:47:10 发布