1

This is the end of my code:

duplicates = [files for files in file_dict.values() if len(files) > 1]

dupString = str(duplicates)

text_file.write("\n\n\nDuplicates Files: \n\n" + (dupString))

The output in the txt file is:

[['/Users/simon/Desktop/aF/file1.py', '/Users/simon/Desktop/aF/hidden/file2.py', '/Users/simon/Desktop/aF/hidden/file3.py']]

I want the output to be:

/Users/simon/Desktop/aF/file1.py
/Users/simon/Desktop/aF/hidden/file2.py
/Users/simon/Desktop/aF/hidden/file3.py

How can I make this happen? I tried using ' '.join(dupString) and '\n'.join(dupString) but it just made the string values have spaces or new lines in which wasn't what I wanted.

tshepang
  • 10,772
  • 21
  • 84
  • 127
BubbleMonster
  • 1,266
  • 6
  • 28
  • 45

1 Answers1

2

Okay, it is my understanding that:

duplicates = [files for files in file_dict.values() if len(files) > 1]

Returns a list of lists of files. Your example being this:

[['/Users/simon/Desktop/aF/file1.py', '/Users/simon/Desktop/aF/hidden/file2.py', '/Users/simon/Desktop/aF/hidden/file3.py']]

In which case, what you need to do is first change that to make only a list of strings, you can do that with itertools.chain.from_iterable

from itertools import chain
duplicates = chain.from_iterable([files for files in file_dict.values() if len(files) > 1])

Then it is just a matter of writing:

text_file.write("\n\n\nDuplicates Files: \n\n%s" % '\n'.join(duplicates)) 
Inbar Rose
  • 35,719
  • 22
  • 80
  • 120