Question: this type of question has been asked many times. So apologies; I have searched hard to get an answer - but have not found anything that is close enough to my needs (and I am not sufficiently advanced **(I am a total newbie)** to customize an existing answer). So thanks in advance for any help.  

Here's my query:  

* I have 30 or so csv files and each contains between 500 and 15,000 rows.
* Within each of them (in the 1st column) - are rows of alphabetical IDs (some contain underscores and some also have numbers).
* I don't care about the unique IDs - but I would like to identify the duplicate IDs and the number of times they appear in all the different csv files.
* Ideally I'd like the output for each duped ID to appear in a new csv file and be listed in 2 columns **("ID", "times\_seen")**

It may be that I need to compile just 1 csv with all the IDs for your code to run properly - so please let me know if I need to do that  

I am using python 2.7 (a crawling script that I run needs this version, apparently).  

Thanks again

Answer: 
It seems the most easy way to achieve want you want would make use of dictionaries.

```
import csv
import os
# Assuming all your csv are in a single directory we will iterate on the 
# files in this directory, selecting only those ending with .csv

# to list files in the directory we will use the walk function in the 
# os module. os.walk(path_to_dir) returns a generator (a lazy iterator)
# this generator generates tuples of the form root_directory, 
# list_of_directories, list_of_files. 
# So: declare the generator
file_generator = os.walk("/path/to/csv/dir")
# get the first values, as we won't recurse in subdirectories, we
# only ned this one
root_dir, list_of_dir, list_of_files = file_generator.next()
# Now, we only keep the files ending with .csv. Let me break that down
csv_list = []
for f in list_of_files:
    if f.endswith(".csv"):
        csv_list.append(f)
# That's what was contained in the line
# csv_list = [f for _, _, f in os.walk("/path/to/csv/dir").next() if f.endswith(".csv")]

# The dictionary (key value map) that will contain the id count.
ref_count = {}
# We loop on all the csv filenames...
for csv_file in csv_list:
    # open the files in read mode
    with open(csv_file, "r") as _:
        # build a csv reader around the file
        csv_reader = csv.reader(_)
        # loop on all the lines of the file, transformed to lists by the 
        # csv reader
        for row in csv_reader:
            # If we haven't encountered this id yet, create 
            # the corresponding entry in the dictionary.
            if not row[0] in ref_count:
                ref_count[row[0]] = 0
            # increment the number of occurrences associated with
            # this id
            ref_count[row[0]]+=1
# now write to csv output
with open("youroutput.csv", "w") as _:
    writer = csv.writer(_)
    for k, v in ref_count.iteritems():
        # as requested we only take duplicates
        if v > 1:
            # use the writer to write the list to the file
            # the delimiters will be added by it.
            writer.writerow([k, v])

```

You may need to tweek a little csv reader and writer options to fit your needs but this should do the trick. You'll find the documentation here <https://docs.python.org/2/library/csv.html>. I haven't tested it though. Correcting the little mistakes that may have occurred is left as a practicing exercise :).
That's rather easy to achieve. It would look something like:

```
import os

# Set to what kind of separator you have. '\t' for TAB
delimiter = ','

# Dictionary to keep count of ids
ids = {}

# Iterate over files in a dir
for in_file in os.listdir(os.curdir):
    # Check whether it is csv file (dummy way but it shall work for you)
    if in_file.endswith('.csv'):
        with open(in_file, 'r') as ifile:
            for line in ifile:
                my_id = line.strip().split(delimiter)[0]
                # If id does not exist in a dict = set count to 0
                if my_id not in ids:
                    ids[my_id] = 0
                # Increment the count
                ids[my_id] += 1

# saves ids and counts to a file
with open('ids_counts.csv', 'w') as ofile:
    for key, val in ids.iteritems():
        # write down counts to a file using same column delimiter
        ofile.write('{}{}{}\n'.format(key, delimiter, value))

```