How i can split some directory from one directory

hi friends
i have one directory that there are a lot of sub directory at it . i want split for example 20% of this sub folder and move them to another directory
please help me .how i can write this code in pytorch or python?

You could use python libs for this use case, e.g. os, shutil etc.
Have a look at this example.
I created some dummy folders, split them randomly by 80-20 and move the 20% split into another folder.
Just change the splitting in the way you would like.

folders_path = os.path.join('YOUR', 'PATH')
folders = np.array(glob.glob(os.path.join(folders_path, '*')))

nb_folders = len(folders)
folder_idx = np.arange(nb_folders)
np.random.shuffle(folder_idx)

folders_train = folders[folder_idx[:int(nb_folders*0.8)]]
folders_test = folders[folder_idx[int(nb_folders*0.8):]]

dest_folder = './'
for folder in folders_test:
    print('Moving {} to {}'.format(folder, dest_folder))
    shutil.move(folder, dest_folder)

thank you for your help.

also i have another question . i have a lot of folders that there are a lot of images at them now i want apply some transforms such as crop and rotate as data augmentation and then i want save this transforms in same folder.
can you help me?

If you would like to store it, you could use Augmentor.

Otherwise, you could apply data augmentation of the fly using a Dataset and DataLoader.
(You can also include augmentor in PyTorch)

1 Like