You want to write unique file names and paths to the text file, duplicate names will have different extensions

advertisements

I am trying to write out the filepath for files with specific file extensions to a text file. There are some files that have different extensions but the same file name, and I am assuming these are duplicates and only want to retain one entry. Here is what I have for code - it is not writing anything out to the file. What am I missing?

import os
path = r'S:\Photogr\ASC'
file_ext_lst = ['.2dm','.2de','.3dm','.3de','.dgn']
txtfile = r'D:\test\microstation_filenames_paths.txt'

for dirpath, dirnames, filenames in os.walk(path):
      for filename in filenames:
              fullPath = os.path.join(dirpath, filename)
              name = os.path.splitext(filename)[0]
              if filename[-4:] in file_ext_lst:
                      with open(txtfile,'r+') as f:
                              for line in f:
                                      if name not in line:
                                              f.write(fullPath +'\n')
                      f.close()


The following code writes duplicate file names and paths to a text file.

import os
# path = r'S:\Photogr\ASC'
path = 'temp'
file_ext_lst = ['.2dm','.2de','.3dm','.3de','.dgn']
txtfile = r'D:\test\microstation_filenames_paths.txt'

found = dict()
for dirpath, _, filenames in os.walk(path):
    for filename in filenames:
        fullPath = os.path.join(dirpath, filename)
        name,ext = os.path.splitext(filename)
        if ext not in file_ext_lst:
            continue
        if name not in found:
            found[name] = fullPath

with open('unique.txt', 'w') as outf:
    print >>outf, 'Unique files:'
    for name,path in found.iteritems():
        print >>outf, '{:<10} {}'.format(name,path)