Write String to txt file

1164
5
Jump to solution
10-06-2016 10:17 AM
jaykapalczynski
Frequent Contributor

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']
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

There may be a better way than this, but you can manually insert line break characters at the end of each write statement. Below will result in different lines but no commas. The commented option will add a comma to every line, including the final line. You could add a little extra logic to check if you're at the final item in the list and not add a comma.

onlyfiles = [f for f in listdir(dir_src) if isfile(join(dir_src, f))]
        
file = open("log.txt", 'w')
for onlyfile in onlyfiles:
    file.write(str(onlyfile) + '\n') # or ',\n' if you want the trailing comma
file.close()

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

There may be a better way than this, but you can manually insert line break characters at the end of each write statement. Below will result in different lines but no commas. The commented option will add a comma to every line, including the final line. You could add a little extra logic to check if you're at the final item in the list and not add a comma.

onlyfiles = [f for f in listdir(dir_src) if isfile(join(dir_src, f))]
        
file = open("log.txt", 'w')
for onlyfile in onlyfiles:
    file.write(str(onlyfile) + '\n') # or ',\n' if you want the trailing comma
file.close()
XanderBakker
Esri Esteemed Contributor

Or if you want it to apear as a "list object" with items on each line, replace ',' with ',\n' in variable onlyfiles2. 

So write onlyfiles2.replace(',', ',\n') instead

jaykapalczynski
Frequent Contributor

Thanks Darren....quick and painless...I was trying this to the variable with no success...perfect....Cheers

0 Kudos
ChristianWells
Esri Regular Contributor

You could use a replace function to change your commas to new line characters:

ls = ['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']

print str(ls).replace(',', '\n')

#Output:
['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']

#If you want to replace the square brackets we can handle that as well:
print str(ls).replace(',', '\n').replace('[', '').replace(']', '')

#Another option:
for i in ls:
    print i

#Output:
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
jaykapalczynski
Frequent Contributor

Thanks Christian....very helpful...much appreciated.

0 Kudos