Reputation: 1063
I'm using Python 2.7 and trying to read from pipe-delimited Src file, adding string to each line and writing the string to another file.
def createInfoTypeFile(srcDir, targetFile, startWith):
for subdir, dir, files in os.walk(srcDir):
for file in files:
fpath = os.path.join(subdir, file)
if (fpath.endswith('.SAP')):
f = open(fpath, 'r')
lines = f.readlines()
for line in lines:
if line.startswith(startWith):
line1 = line + "|key|false\n"
targetFile.write(line1)
I call the above code, passing it the src, target files and a starting string
The string gets appended to the line, but after a new line
a|b|
|key|false
'key|false|' - is what i'm adding to each Line read, but after newline I want |key|false added to the original line w/o newline
i.e.
a|b|key|false
Upvotes: 0
Views: 48
Reputation: 21305
line1 = line.rstrip() + "|key|false\n"
Strip out the extra newline from the original before appending.
Upvotes: 1