-2

I have a folder with multiple subfolders (with more subfolders in those) where different specifications of datasets are saved (sometimes sliced and diced).

I am afraid that some legacy files in there will mess things up when I aggregate files together. So I am looking for a command to delete all files in all subfolders of a given folders (but not the subfolders them selves). Is there a simple way of doing this in R, Python or Stata without having to create a list of all applicable subfolders first?

safex
  • 2,398
  • 17
  • 40
  • 1
    So you have a top level folder with subfolders, and you want to delete all the files in these subfolders but keep the subfolders? With questions like these its always good to give a concrete example. – RoadRunner Dec 03 '18 at 11:12

1 Answers1

3

Python

import os
for root, dirs, files in os.walk('/path/to/root/folder/'):
    for file in files:
        print(file)
        os.remove(file)

R

root_dir <- "/path/to/root/folder"
files <- paste(root_dir, list.files(root_dir, recursive = T), sep="/")
for(file_path in files){
  file.remove(file_path)
}
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42