See comments inline
# initialize the variable to None, it could have been "" also..
prev_base = None
#Iterates through the directory containing the files sorted.
#It assumes that you are running the script in same directory as files.
for rs_file in sorted(os.listdir('.') ):
# Extract the identical part from each basename.
# Example: for qqqqwwwwrrrr.txt.54321987, base_rs_file=qqqqwwwwrrrr.txt
There is a problem here, it should be -9 not -7
base_rs_file = rs_file[:-9]
# Compare to previous basename. This is why prev_base had to be initialized. Otherwise # test would return an error
if base_rs_file == prev_base:
# The basename has already been found, the file is removed.
# The method unlink() removes (deletes) the file path
os.unlink( rs_file)
else:
# if the basename has not been found previously, value is assigned to prev_base
prev_base = base_rs_file