Q:

Delete every n-th file Python

I'm looking to delete every 10th file from a folder using a Python script.  I looked around and found this code:
import os
l = os.listdir('/Users/myname/Desktop/TBD')

for n in l[::10]:
    os.unlink(n)

I created a Python script for this then ran it in terminal using 
python filename.py

And this is what I get in return:
FileNotFoundError: [Errno 2] No such file or directory: 'Pic-1.jpg'

The folder I point to with the code is the folder containing 1000 photos.  It's reading the name of the first folder but isn't going through with the delete process.  Is there a better way of doing this? 

A:

It's because listdir() is returning you just names of entries in specified folder, without path element.  So when you try to unlink these files, you try to do that in context of current working directory, not in /Users/myname/Desktop/TBD/ as you should.  Simply add path to each filename you want to unlink or ensure /Users/myname/Desktop/TBD is your working directory.  
Additionally, for general safety you should check if element you are about to unlink is in fact a file, not i.e. directory or anything else:
import os

dir_to_clean = '/Users/myname/Desktop/TBD'
l = os.listdir(dir_to_clean)

for n in l[::10]:
    target = dir_to_clean + '/' + n
    if os.path.isfile(target):
        os.unlink(target)

