在python中批量移动10个文件

mnowg1ta  于 2023-08-02  发布在  Python
关注(0)|答案(1)|浏览(90)

我正在写一个Python脚本,它有2个文件夹,一个是source_folder另一个是destination_folder,我想要实现的是从源文件夹我应该能够移动x个文件到destination_folder,其中X是从数据库获取数据的变量,所以例如我想要x = 10,它应该在x秒或分钟后的给定时间点只复制10个文件,我可以移动,但不是成批的
这是我代码,

import os
import shutil
 
source = 'callfiles/processed/'
destination = 'callfiles/created/'
 
# gather all files
allfiles = os.listdir(source)
 
# iterate on all files to move them to destination folder
for f in allfiles:
    src_path = os.path.join(source, f)
    dst_path = os.path.join(destination, f)
    shutil.move(src_path, dst_path)

字符串
关于CJ

pcww981p

pcww981p1#

Got This done via below code

import shutil
import os
import glob
import time

def copy_three_files(source_dir, destination_dir):
    # Get a list of all files in the source directory
    source_files = glob.glob(os.path.join(source_dir, '*'))

    # Process files three at a time
    for i in range(0, len(source_files), 30):
        # Get the next three files
        files_to_copy = source_files[i:i+30]

        # Copy each file to the destination directory
        for file_path in files_to_copy:
            file_name = os.path.basename(file_path)
            destination_path = os.path.join(destination_dir, file_name)
            time.sleep(0.04)
            shutil.move(file_path, destination_path)

        print(f"Copied {len(files_to_copy)} files: {', '.join(files_to_copy)}")

if __name__ == "__main__":
    # Replace these paths with your desired source and destination directories
    source_directory = "<path to src>"
    destination_directory = "<path to dest>"
    
    copy_three_files(source_directory, destination_directory)

字符串

相关问题