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

从终端中的python脚本缩进txt文件,python,Python,我正在尝试编写一个python脚本,如果当前没有txt文件,它将生成一个txt文件,并在其中添加“将我缩进下一行的某个位置”这段文字。我想知道文本如何在字符串的中间缩进到下一行,用最少的Python。谢谢大家 import sys if not os.path.exists(/Users/you/Desktop/AIMemory.txt) : memory = open("AIMemory.txt","w") print("Created A Ne

我正在尝试编写一个python脚本,如果当前没有txt文件,它将生成一个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")

需要在末尾添加制表符和换行符,如:

memory.write("\tIndent Me Somewhere Onto The Next Line\n")
你需要把
内存。写入(“\t将我插入下一行\n”)


memory.write(“\n\t将我插入下一行”)
,如果您希望第一行为空。

尝试将“\t”放在“Ident me…”之前,这似乎对我不起作用,但它会缩进文本,不会在结尾处将其推到新的行上放置“\n”非常感谢。我不知道为什么我没有想到这一点。