AnsweredAssumed Answered

Write String to txt file

Question asked by jay.kapalczynski on Oct 6, 2016
Latest reply on Oct 6, 2016 by jay.kapalczynski

I have a string that is being created from a listdir.  It is Comma delimited...

I want to write this to the txt file as separate lines.  NOTING that each time I run this there will a different number in the list.  So I cannot count on any indexing etc.

 

What can I do to write this to separate lines one for each of the files located from the listdir?

 

THANKS

 

Code:
#...snip
dir_src = "C:\\users\\tjv36463\\Desktop\\BearCollar\\NewFiles\\"
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(dir_src) if isfile(join(dir_src, f))]
onlyfiles2 = str(onlyfiles)
       
file = open("log.txt", 'w')
file.write(onlyfiles2)
file.close()
#...snip

Result:
['rangedate_D032495_20160822124736.txt', 'rangedate_D032498_20160822125645.txt', 'rangedate_D032499_20160822125129.txt', 'rangedate_D032500_20160822125032.txt', 'rangedate_D032501_20160822125559.txt', 'rangedate_D032502_20160822125356.txt', 'rangedate_D033108_20160822125458.txt']

Im looking for:
['rangedate_D032495_20160822124736.txt',
'rangedate_D032498_20160822125645.txt',
'rangedate_D032499_20160822125129.txt',
'rangedate_D032500_20160822125032.txt',
'rangedate_D032501_20160822125559.txt',
'rangedate_D032502_20160822125356.txt',
'rangedate_D033108_20160822125458.txt']

Outcomes