import glob
import os
filelist=glob.glob("/home/test/*.txt")
for file in filelist:
os.remove(file)
I'm able to delete all files through above code. But I don't want to delete latest 2 files out of 10 txt files.Rest of them wanted to be deleted. Can someone help me please?
Edit:
I tried index
to exclude last 2 files, Got different output. Files
-rwxrwxr-x 1 test1 test 14 May 27 2015 test.txt
-rw-r--r-- 1 test1 test 1857 Nov 9 2016 list.txt
-rw-r--r-- 1 test1 test 140 Jun 8 22:09 check.txt
-rw-r--r-- 1 test1 test 570 Jun 8 22:12 ert.txt
-rw-r--r-- 1 test1 test 0 Jul 2 03:17 1.txt
-rw-r--r-- 1 test1 test 0 Jul 2 03:17 2.txt
My new code is:
import glob import os
filelist=glob.glob("/home/test/*.txt")
for file in filelist[:-2]:
print file
Output
> /home/test/1.txt
> /home/test/2.txt
> /home/test/list.txt
> /home/test/ert.txt