Python reading files in a directory
Question: I have a .csv with 3000 rows of data in 2 columns like this:

    
    
    uc007ayl.1  ENSMUSG00000041439
    uc009mkn.1  ENSMUSG00000031708
    uc009mkn.1  ENSMUSG00000035491
    

In another folder I have a graphs with name like this:

    
    
    uc007csg.1_nt_counts.txt
    uc007gjg.1_nt_counts.txt
    

You should notice those graphs have a name in the same format of my 1st column

I am trying to use python to identify those rows that have a graph and print
the name of 2nd column in a new .txt file

These are the codes I have

    
    
    import csv
    with open("C:/*my dir*/UCSC to Ensembl.csv", "r") as f:
    reader = csv.reader(f, delimiter = ',')
        for row in reader:
            print row[0]
    

But this as far as I can get and I am stuck.


Answer: You're almost there:

    
    
    import csv
    import os.path
    with open("C:/*my dir*/UCSC to Ensembl.csv", "rb") as f:
        reader = csv.reader(f, delimiter = ',')
        for row in reader:
            graph_filename = os.path.join("C:/folder", row[0] + "_nt_counts.txt")
            if os.path.exists(graph_filename):
                print (row[1])
    

Note that the repeated calls to
[`os.path.exists`](http://docs.python.org/library/os.path.html#os.path.exists)
may slow down the process, especially if the directory lies on a remote
filesystem and does not significantly more files than the number of lines in
the CSV file. You may want to use
[`os.listdir`](http://stackoverflow.com/a/11738276/4279) instead:

    
    
    import csv
    import os
    
    graphs = set(os.listdir("C:/graph folder"))
    with open("C:/*my dir*/UCSC to Ensembl.csv", "rb") as f:
        reader = csv.reader(f, delimiter = ',')
        for row in reader:
            if row[0] + "_nt_counts.txt" in graphs:
                print (row[1])
    

