How to extract exif data into a csv file?
<pre><code>import pyexiv2
import os

print "Enter the full path to the directory that your images are conatined in."
print "-------------------------------------------"
newFileObj = open('C:\\users\\wilson\\desktop\\Metadata.csv', 'w')
targ_dir = raw_input('Path: ')

targ_files = os.listdir(targ_dir)

def getEXIFdata (imageFile):
    if imageFile.endswith(".db"):
        f = 1
    else:

        EXIFData = pyexiv2.ImageMetadata(imageFile)
        EXIFData.read()
        CamMake = EXIFData['Exif.Image.Make']
        DateTime = EXIFData['Exif.Image.DateTime']
        CamModel = EXIFData['Exif.Image.Model']
    for image in targ_files:
        getEXIFdata(targ_dir+"\\"+img)
        newFileObj.write(DateTime+' , '+CamMake+' , '+CamModel+'\r\n')
newFileObj.close()

end = raw_input("Press Enter to Finish: ")
</code></pre>

<p>This is what I have so far, but I just dont understand how to actually get the data into the file.  It crates the file, but it is just blank.  I've tried moving around the bottom for, but I just can't seem to get it to work.  I am new to python, so please if you could keep it simple when you hint at what I should do.</p>

<p>If you want to get data of exif, use <code>.value</code> to get value of data.
Here is an example.</p>

<pre><code># -*-coding:utf-8-*-
import sys
import csv
import os
import argparse
import pyexiv2

def main():
    parser = argparse.ArgumentParser(description="Change the txt file to csv.")
    parser.add_argument("-i", action="store", dest="infile")
    parser.add_argument("-o", action="store", dest="outfile")
    parser_argument = parser.parse_args()

    fatherdir = os.getcwd()  # code directory
    inputfile = outputfile = None

    # input exif file
    if parser_argument.infile:
        infilepaths = os.path.split(parser_argument.infile)
        # 'C:\User\lenovo\Desktop\pakistan.txt' ---&gt; ['C:\User\lenovo\Desktop','pakistan.txt']
        if infilepaths[0]:  # full path
            inputfile = parser_argument.infile
            fatherdir = infilepaths[0]
        # 'pakistan.txt' ---&gt; ['','pakistan.txt']
        else:  # only file name
            inputfile = fatherdir + '/' + parser_argument.infile
    # output csv file
    if parser_argument.outfile:
        outfilepaths = os.path.split(parser_argument.outfile)
        if outfilepaths[0]:  # full path
            outputfile = parser_argument.outfile
        else:
            outputfile = fatherdir + '/' + parser_argument.outfile
    else:
        outputfile = fatherdir + '/test_csv.csv'
    parse(inputfile, outputfile)


def parse(inputfile, outputfile):
    csvcontent = file(outputfile, 'wb')
    writer = csv.writer(csvcontent)

    exif_data = getEXIFdata(inputfile)
    writer.writerow([exif_data['Exif.Image.Orientation'].value,
                     exif_data['Exif.Photo.PixelXDimension'].value,
                     exif_data['Exif.Photo.PixelYDimension'].value])
    # for line in open(inputfile).readlines():
    #     writer.writerow([a for a in line.split('\t')])

    csvcontent.close()

def getEXIFdata (imageFile):
    if imageFile.endswith(".db"):
        print 'Skip this file'
    else:
        exif_data = pyexiv2.ImageMetadata(imageFile)
        exif_data.read()
        for s, v in exif_data.items():
            print s, v
        cam_a = exif_data['Exif.Image.Orientation'].value
        cam_b = exif_data['Exif.Photo.PixelXDimension'].value
        cam_c = exif_data['Exif.Photo.PixelYDimension'].value
        # add exif value
        ekey = 'Exif.Photo.UserComment'
        evalue = 'A comment.'
        exif_data[ekey] = pyexiv2.ExifTag(ekey, evalue)
        #metadata.write()
        return exif_data

if __name__ == '__main__':
    main()
</code></pre>

<p>run this code like this </p>

<pre><code>python exif2csv.py -i wifi.jpg -o demo_csv.csv
</code></pre>

<p>If you want to loop files in directory, I think you can figure it out by yourself.</p>

python csv metadata exif pyexiv2