1

Im making script to find directory paths from file to form ant mkdir elements from that.

The problem is that when I print at the end there is linebreak added inside each line:

ant_mkdir = '<mkdir dir="..' + path + '"/>'

code:

from io import open
from string import *
def main():
    with open("file.txt", "r") as f:
        content = f.readlines()

    paths = []
    for line in content:
        if ("d:\\apps" in line):
            line = line.split("d:\\apps")
            path = line[1]
            path = path.replace("\\", "/")
            if path not in paths:
                paths.append(path)

    for path in paths:
        ant_mkdir = '<mkdir dir="..' + path + '"/>'
        print ant_mkdir

if __name__ == "__main__":
    main()

print result:

<mkdir dir="../path/folder/1
"/>
<mkdir dir="../path/folder/2
"/>
<mkdir dir="../path/folder/3
"/>
osguosgu
  • 95
  • 5
  • 1
    The problem is that there are newlines on every line of the file that you read. See if this helps: http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python – darthbith May 14 '14 at 12:08

1 Answers1

1

Try:

 ant_mkdir = '<mkdir dir="..' + path.rstrip() + '"/>'

to remove whitespaces and linebreaks.

osguosgu
  • 95
  • 5
Josip Grggurica
  • 421
  • 4
  • 12