【发布时间】:2016-09-15 17:26:23
【问题描述】:
# 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)
我上面的代码清楚地向我正在编辑的文件写入了一个换行符。当我尝试使用该文件时,另一个脚本不会执行该行,因为文件末尾没有换行符。如果我复制文件然后在 vi 中打开它然后保存它,vi 添加一个换行符并且文件的行正确执行。如果我在未保存在 vi 中的文件与保存在 vi 中的文件之间运行差异,我会收到消息,即文件末尾没有换行符。我在 Ubuntu 服务器 Xenial 上使用 Python 2.7.12。
【问题讨论】:
-
hexdump -Cv fileDestPath的输出是什么? -
hexdump 如下.... 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
-
如您所见,换行符在其中(末尾的
0a)。由于我从未理解的原因,vi在编辑文件时不显示最后一个换行符。这个question about vi 可能有用。
标签: python