2
# Create strings for preparation of file writing
s = ";"
seq = (risd41Email, risd41Pass, rimsd41Email, rimsd41Pass);
textString = s.join(seq);

# Create file, write contents, move to usertxtfiles dir
with open(filename, "w") as text_file:
    text_file.write(str(textString))
    text_file.write('\n')

os.rename(fileMigPath, fileDestPath)

I have the above code that clearly writes a newline to the file I am editing. When I try to use the file another script does not execute the line because there is no newline character at the end of the file. If I copy the file and then open it in vi and then save it, vi adds a newline character and the line of the file executes properly. If I run diff between the file that wasn't saved in vi versus the one that was I get the message that the difference is no newline at end of file. I am using Python 2.7.12 on Ubuntu server Xenial.

mpmackenna
  • 403
  • 1
  • 6
  • 20
  • What is the output of `hexdump -Cv fileDestPath`? –  Sep 15 '16 at 17:31
  • hexdump as follows.... 00000000 6d 69 6b 65 2e 6d 61 63 40 6f 6c 64 64 6f 6d 61 |mike.mac@olddoma| 00000010 69 6e 2e 6f 72 67 3b 6e 6f 74 61 70 61 73 73 77 |in.org;notapassw| 00000020 6f 72 64 3b 6d 69 6b 65 2e 6d 61 63 40 6e 65 77 |ord;mike.mac@new| 00000030 64 6f 6d 61 69 6e 2e 6f 72 67 3b 6e 6f 74 61 6e |domain.org;notan| 00000040 65 77 70 61 73 73 0a |ewpass.| 00000047 – mpmackenna Sep 15 '16 at 17:53
  • As you can see, the newline is in there (the `0a` at the end). For reasons I have never understood, `vi` does not show the the last newline when editing a file. This [question about vi](http://stackoverflow.com/questions/19240082/why-do-gedit-and-vim-hide-the-final-newline-from-the-user) might be useful. –  Sep 15 '16 at 17:55

1 Answers1

1

Try that:

    text_file.write('\n\n')

This should work!

coder
  • 12,832
  • 5
  • 39
  • 53