I'm still pretty new to python and trying my best to teach myself. What I'm trying to do is get 3 different file paths written to a csv after iterating through a folder. I'm having an issue in that only the first file path that should print is printing. I'm hoping to get the following output printed to a csv:
['C:\\Users\\micha\\Documents\\USGS-NHD DATA\\NHDwaterbody.shp']
['C:\\Users\\micha\\Documents\\USGS-NHD DATA\\VietnamPA_name__Tam Dao.shp']
['C:\\Users\\micha\\Documents\\USGS-NHD DATA\\WBD_11.shp']
But I end up only getting one this as my output:
C:\Users\micha\Documents\USGS-NHD DATA\VietnamPA_name__Tam Dao.shp
This is the code I've come up with currently. I'd appreciate any input as to how I could get all three items to print to a csv instead of just the one.
import os
import csv
# assign directory
directory = 'C:\\Users\\micha\\Documents\\USGS-NHD DATA'
# iterate over files in
# that directory
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
# checking if it is a file shp
if f.endswith('.shp'):
data= [f]
n = open('C:\\Users\\micha\\Documents\\create.csv','w')
writer = csv.writer(n)
for item in data:
writer.writerows([data])