Python:逐行将文本写入文件

Python:逐行将文本写入文件,python,file,Python,File,我正在尝试将一些文本写入文件,以下是我尝试的内容: text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \ "industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \ " when an unknown pr

我正在尝试将一些文本写入文件,以下是我尝试的内容:

text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \
                  "industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \
                  " when an unknown printer took a galley of type and scrambled it to make a type specimen book."
target = open("file", 'wb')
target.writelines(text)
我得到一个空文件。
我如何才能做到这一点?

writelines
需要一个可数行(例如列表),所以不要使用它。您需要关闭文件以保存更改,最好使用
with
语句:

with open("file", 'wb') as target:
    target.write(text)

以下是如何打印到txt文件:

file = open("Exported.txt", "w")
file.write("Text to write to file")
file.close() #This close() is important
import os.path

def start():

    print("What do you want to do?")
    print("    Type a to write a file")
    print("    Type b to read a file")
    choice = input("            -")
    if choice == "a":
        create()
    elif choice == "b":
        read()
    else:
        print("Incorrect spelling of a or b\n\n")
        start()


def create():

    print()
    filename = input("What do you want the file to be called?\n")
    if os.path.isfile(filename):
        print("This file already exists")
        print("Are you sure you would like to overwrite?")
        overwrite = input("y or n")
        if overwrite == "y":
            print("File has been overwritten")
            write(filename)
        else:
            print("I will restart the program for you")
    elif not os.path.isfile(filename):
        print("The file has not yet been created")
        write(filename)
    else:
        print("Error")





def write(filename):
    print()
    print("What would you like the word to end writing to be?")
    keyword = input()
    print("What would you like in your file?")
    text = ""
    filename = open(filename, 'w')
    while text != keyword:
        filename.write(text)
        filename.write("\n")
        text = input()


def read():
    print()
    print("You are now in the reading area")
    filename = input("Please enter your file name:     -")
    if os.path.isfile(filename):
        filename = open(filename, 'r')
        print(filename.read())
    elif not os.path.isfile(filename):
        print("The file does not exist\n\n")
        start()
    else:
        print("Error")


start()
另一种方法是:

with open('Exported.txt', 'w') as file:
   file.write("Text to write to file")
这是我编写的一个程序,用于编写txt文件:

file = open("Exported.txt", "w")
file.write("Text to write to file")
file.close() #This close() is important
import os.path

def start():

    print("What do you want to do?")
    print("    Type a to write a file")
    print("    Type b to read a file")
    choice = input("            -")
    if choice == "a":
        create()
    elif choice == "b":
        read()
    else:
        print("Incorrect spelling of a or b\n\n")
        start()


def create():

    print()
    filename = input("What do you want the file to be called?\n")
    if os.path.isfile(filename):
        print("This file already exists")
        print("Are you sure you would like to overwrite?")
        overwrite = input("y or n")
        if overwrite == "y":
            print("File has been overwritten")
            write(filename)
        else:
            print("I will restart the program for you")
    elif not os.path.isfile(filename):
        print("The file has not yet been created")
        write(filename)
    else:
        print("Error")





def write(filename):
    print()
    print("What would you like the word to end writing to be?")
    keyword = input()
    print("What would you like in your file?")
    text = ""
    filename = open(filename, 'w')
    while text != keyword:
        filename.write(text)
        filename.write("\n")
        text = input()


def read():
    print()
    print("You are now in the reading area")
    filename = input("Please enter your file name:     -")
    if os.path.isfile(filename):
        filename = open(filename, 'r')
        print(filename.read())
    elif not os.path.isfile(filename):
        print("The file does not exist\n\n")
        start()
    else:
        print("Error")


start()

您提供的代码生成一个名为file的文件,其中包含所需的行。也许您想将其保存为“file.txt”。另外,
'wb'
标志中的
'b'
告诉代码以二进制模式写入文件(更多信息)。如果希望文件可读,请尝试使用
'w'

最后,在访问文件时最好使用
with
语句

text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \
              "industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \
              " when an unknown printer took a galley of type and scrambled it to make a type specimen book."
with open("file.txt", 'w') as f:
    f.write(text)

通过这种方式,您应该直接关闭文件:

target = open("filename.txt", 'w')
target.writelines(text)
target.close()
通过这种方式,在
with
完成执行后,在缩进块后关闭文件:

with open("filename.txt", "w") as fh:
    fh.write(text)
更多信息:


之后您是否忘记了
.close()
文件?而且,您的文本只有一行。
target.close()
将保存它。我已经编写了如何打印到文本文件,以及如何在代码中正确使用它。希望这有帮助。是的,问题在于结束,我也将writelines改为write,效果很好。@Ashref如果我的代码有帮助,请向上投票,如果它回答了你的问题,请按绿色勾号将其标记为答案。然后其他有同样问题的人也可以找到这个答案来帮助他们解决问题。谢谢另外,我还确保对你的问题进行投票:)