0

I have a folder where want to delete the files on it when they reaches 5 files (or if they are older than 5 days). so far I have tried this:

path_file = r'C:\...\new folder'
files = os.listdir(path_file)
for file in files [:5]:
    os.remove(file)

This code gives the next error:

os.remove(file)
FileNotFoundError: [WinError 2] The system cannot find the file specified file
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251

1 Answers1

0

Add path to the filename

path= r'C:\...\new folder' 
files = os.listdir(path_file) 
for file in files [:5]: 
    os.remove(path + "\\" + file)
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
geekay
  • 340
  • 1
  • 5