在python中写入文件后,文件处理程序未打印

时间:2019-07-22 10:00:32

标签: python file-io

我正在尝试写入一个文件并在写入后读取。下面是我的代码。

with open('1.txt','w') as fr:
    x = '3'
    fr.write(x)
    with open('1.txt','r') as fr_r:
        fr_w = fr_r.read()
        print (fr_w)

预期输出低于

3

2 个答案:

答案 0 :(得分:0)

您的代码应阅读(第二个没有缩进)

with open('1.txt','w') as fr:
    x = '3'
    fr.write(x)
with open('1.txt','r') as fr_r:
    fr_w = fr_r.read()
    print (fr_w)

答案 1 :(得分:0)

一旦您用'with'语句打开了一个文件。您无需关闭该频道。因此,python开发人员在此块中设置了禁止写入或使用其他权限通道的限制。

                   once read /write permission in opened we cannot add another permission channel in that block or indentation. it should be in seperate indentation like this below as follows``