0

I have a list of pathnames:

li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]

I am trying to write each path on a separate line in a txt file. This is what I have done so far:

import csv

li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]

with open(r'C:\temp\myfile.csv', "wb") as f:
    wr = csv.writer(f, delimiter=',', quoting=csv.QUOTE_NONE)
    wr.writerows([li])

Which yields a list of files on the same row:

C:\temp\fileA.shp,C:\temp\fileB.shp,C:\temp\fileC.shp

How can I tweak this so that the pathnames are each on their own row? The following is what I am after:

C:\temp\fileA.shp
C:\temp\fileB.shp
C:\temp\fileC.shp
Borealis
  • 8,044
  • 17
  • 64
  • 112
  • The linked question has the answer to your problem. If you are directly writing to a txt file, you do not need to use CSV. – Bhargav Rao Jul 18 '15 at 05:00

1 Answers1

-1

Easy just need to add \n witch means new

import csv

li = [u"C:\\temp\\fileA.shp", u"C:\\temp\\fileB.shp", u"C:\\temp\\fileC.shp"]

with open(r'C:\temp\myfile.txt', "wb") as f:
    wr = csv.writer(f + '\n', delimiter=',', quoting=csv.QUOTE_NONE)
    wr.writerows([li])

So now f will be printed + \n (new line)

Shaggy89
  • 689
  • 1
  • 5
  • 18