python os文件夹路径切分;shutil文件复制到其他文件夹

1、os文件夹路径切分

import os

path = 'path/to/some/folder/filename.txt'
filename = os.path.split(path)[-1]  # 取最后一个元素
print(filename)  # 输出 filename.txt

2、shutil文件复制到其他文件夹

import os
import shutil

src_folder = 'path/to/source/folder'
dst_folder = 'path/to/destination/folder'

for filename in os.listdir(src_folder):
    src = os.path.join(src_folder, filename)
    dst = os.path.join(dst_folder, filename.lower())  # 将目标文件名改为小写
    if os.path.isfile(src):
        shutil.copy(src, dst)
        print(f"{src} has been copied to {dst}")
import shutil

for name in df_list:
    ii=0
    for people in face_lists:
        if name in people:
            folder =  os.path.split(people)[0]
            filename = os.path.split(people)[-1]
            src = os.path.join(folder, filename)
            dst = os.path.join(r"C:\Users\loong\Downloads\face_db", filename.split("_")[0]+".jpg") 
            if os.path.isfile(src):
                shutil.copy(src, dst)
            ii+=1
    if ii==0:
        print("未找到:",name)
    
        
        

你可能感兴趣的:(知识点,python,python,数学建模,开发语言)