how to move files in directories in group of 3

duhaiyun2010 注册会员
2023-01-24 21:55
import os
import shutil

# Get the list of files in the current directory
files = os.listdir()

# Filter the list of files to only include regular files
files = [f for f in files if os.path.isfile(f)]

# Create a list of folders to move the files to
folders = [f"folder{i+1}" for i in range(20)]

# Loop over each group of three files
for i in range(0, len(files), 3):
    # Create a path for each file in the group
    file_paths = [os.path.join(os.getcwd(), files[j]) for j in range(i, i+3)]

    # Move the group of files to the next folder
    for path in file_paths:
        shutil.move(path, folders[i // 3])

About the Author

Question Info

Publish Time
2023-01-24 21:55
Update Time
2023-01-24 21:55