如何在python中写入和读取文件?

如何在python中写入和读取文件?,python,list,file,file-writing,Python,List,File,File Writing,以下是我的代码到目前为止的样子: restart = 'y' while (True): sentence = input("What is your sentence?: ") sentence_split = sentence.split() sentence2 = [0] print(sentence) for count, i in enumerate(sentence_split): if sentence_split.cou

以下是我的代码到目前为止的样子:

restart = 'y'
while (True):
    sentence = input("What is your sentence?: ")
    sentence_split = sentence.split() 
    sentence2 = [0]
    print(sentence)
    for count, i in enumerate(sentence_split): 
        if sentence_split.count(i) < 2:
            sentence2.append(max(sentence2) + 1)
        else:
            sentence2.append(sentence_split.index(i) +1)
    sentence2.remove(0)
    print(sentence2)
    outfile = open("testing.txt", "wt")
    outfile.write(sentence)
    outfile.close()
    print (outfile)
    restart = input("would you like restart the programme y/n?").lower()
    if (restart == "n"):
            print ("programme terminated")
            break
    elif (restart == "y"):
        pass
    else:
        print ("Please enter y or n")
restart='y'
虽然(正确):
句子=输入(“你的句子是什么?:”)
句子分割=句子分割()
句子2=[0]
打印(句子)
对于计数,我在枚举(句子分割):
如果句子分割,则计数(i)<2:
句子2.附加(最大(句子2)+1)
其他:
句子2.附加(句子分割索引(i)+1)
句子2.删除(0)
打印(第2句)
outfile=open(“testing.txt”、“wt”)
写出(句子)
outfile.close()
打印(输出文件)
restart=input(“您想重新启动程序吗?”)。lower()
如果(重新启动==“n”):
打印(“节目终止”)
打破
elif(重新启动==“y”):
通过
其他:
打印(“请输入y或n”)

我需要知道怎么做,以便我的程序打开一个文件,保存输入的句子和重新创建句子的数字,然后能够打印文件。(我猜这就是阅读部分)。正如你可能知道的,我对文件的读写一无所知,所以请写下你的答案,让一个noob能够理解。另外,代码中与文件相关的部分是一个完整的暗中刺杀,取自不同的网站,所以不要认为我对此有所了解

基本上,您可以通过打开文件对象来创建文件对象,然后执行读或写操作

从文件中读取一行

#open("filename","mode")
outfile = open("testing.txt", "r")
outfile.readline(sentence)
从文件中读取所有行的步骤

for line in fileobject:
    print(line, end='')
使用python编写文件的步骤

outfile = open("testing.txt", "w")
outfile.write(sentence)

基本上,您可以通过打开文件对象来创建它,然后执行读或写操作

从文件中读取一行

#open("filename","mode")
outfile = open("testing.txt", "r")
outfile.readline(sentence)
从文件中读取所有行的步骤

for line in fileobject:
    print(line, end='')
使用python编写文件的步骤

outfile = open("testing.txt", "w")
outfile.write(sentence)

简单地说,要在python中读取文件,您需要在读取模式下“打开”该文件:

f = open("testing.txt", "r")
第二个参数“r”表示我们打开文件进行读取。拥有文件对象“f”后,可以通过以下方式访问文件内容:

content = f.read()
要用python编写文件,需要以写模式(“w”)或附加模式(“a”)打开文件。如果选择写入模式,文件中的旧内容将丢失。如果选择附加模式,新内容将写入文件末尾:

f = open("testing.txt", "w")
要将字符串s写入该文件,我们使用write命令:

f.write(s)
在您的情况下,它可能类似于:

outfile = open("testing.txt", "a")
outfile.write(sentence)
outfile.close()

readfile = open("testing.txt", "r")
print (readfile.read())
readfile.close()

我建议遵循cricket_007指出的官方文档:

简单地说,要用python读取文件,您需要以读取模式“打开”文件:

f = open("testing.txt", "r")
第二个参数“r”表示我们打开文件进行读取。拥有文件对象“f”后,可以通过以下方式访问文件内容:

content = f.read()
要用python编写文件,需要以写模式(“w”)或附加模式(“a”)打开文件。如果选择写入模式,文件中的旧内容将丢失。如果选择附加模式,新内容将写入文件末尾:

f = open("testing.txt", "w")
要将字符串s写入该文件,我们使用write命令:

f.write(s)
在您的情况下,它可能类似于:

outfile = open("testing.txt", "a")
outfile.write(sentence)
outfile.close()

readfile = open("testing.txt", "r")
print (readfile.read())
readfile.close()

我建议遵循cricket_007指出的官方文档:

不要从随机网站获取,无论如何使用,您可能希望在while循环之前打开文件,然后关闭。不要从随机网站获取,无论如何使用,您可能希望在while循环之前打开文件,然后关闭它们。OP似乎正在使用Python 3.x,因此使用上下文管理器
将是打开和关闭文件的更好选项。OP似乎正在使用Python 3.x,因此使用上下文管理器
将是打开和关闭文件的更好选项。