python移动文件(将一个文件夹里面的文件移动到另一个文件夹,支持指定文件移动)

 move_specific_files函数接受一个新的可选参数files_to_move,它是一个包含文件名的列表。如果这个参数被提供,函数只会移动列表中的文件。如果没有提供(即默认为None),则会移动源目录中的所有文件。这样,你就可以根据需要选择性地移动文件了。

import shutil
import os

def move_specific_files(old_path, new_path, files_to_move=None):
    """
    Move specific files from old_path to new_path.
    
    :param old_path: The source directory from where files are to be moved.
    :param new_path: The destination directory to which files are to be moved.
    :param files_to_move: A list of filenames to be moved. If None, all files in old_path are moved.
    """
    print("Source Directory:", old_path)
    print("Destination Directory:", new_path)
    
    if files_to_move is None:
        # If no specific files are mentioned, move all files
        filelist = os.listdir(old_path)
    else:
        # Only consider the specified file

你可能感兴趣的:(P神重生之路,python)