从终端中的python脚本缩进txt文件

时间:2016-05-28 19:18:05

标签: python

我试图编写一个python脚本,如果没有当前文件,将生成一个txt文件,并在此文本中添加“将我缩进到下一行的某个地方”。我想知道文本如何在字符串的中间缩进到具有最少量python的下一行。谢谢大家。

    import sys
    if not os.path.exists(/Users/you/Desktop/AIMemory.txt) :
        memory = open("AIMemory.txt","w")
        print("Created A New Memory")
    else :
        print("Accessing Memory")
        memory = open("AIMemory.txt", "r")
        print(memory.read())
        memory.close()
    memory = open("AIMemory.txt", "a")
    memory.write("Indent Me Somewhere Onto The Next Line")

2 个答案:

答案 0 :(得分:1)

您需要在最后添加制表符和换行符,例如:

Visual Studio 2013 v12.0.40629 Update5

答案 1 :(得分:0)

你需要放 memory.write("\tIndent me onto the next line \n")

OR

memory.write("\n\tIndent me onto the next line "),如果您希望第一行为空白。