Copying specific file amount

44 views Asked by At

When using google colab with python language. How exactly do I copy specific amount of data like 100 file (from 900 file) from downloaded directory to another directory? Since I use this command it copying all file din directory

!cp /content/dataset/train/rottenapples/*.png /content/fresh_rotten/rotten_pic

Is it using loop with specific range?

1

There are 1 answers

0
sanjusci On BEST ANSWER
import os
import shutil

# Source directory containing the 900 files
source_dir = '/content/dataset/train/rottenapples/'

# Destination directory where you want to copy the files
destination_dir = '/content/fresh_rotten/rotten_pic/'

# List all files in the source directory
files = os.listdir(source_dir)

# Copy the first 100 files from the source directory to the destination directory
for file_name in files[:100]:
    source_file = os.path.join(source_dir, file_name)
    shutil.copy(source_file, destination_dir)

You can run this code in a code cell in Google Colab to copy the specified number of files. Adjust the paths (source_dir and destination_dir) according to your actual directory structure.