为什么我得到一个空文件时,试图使用python编辑它?



我正在尝试使用python编辑一个html文本文件。打印它时,它给出一个空文件。为什么它是空的?我试着把它打印出来,因为我不知道怎么退货。代码如下:

import bleach 
with open ('index1.txt','w') as f: #to open the file that contains html markups

bleach.clean( 
'f',
tags=['p'],
attributes=['style'],
styles=['color'], 
)    

f=open('index1.txt')
content = f.read()
f.close()
print(content)

它变为空是因为您打开文件时使用'w'写入,因此根据文档将其设置为空-只需将其更改为'r'或'a'

它将保持为空,因为你只是在创建一个文件&没有写任何东西。

最新更新