Home > other >  Why do i get an empty file when trying to edit it using pyhon? [duplicate]
Why do i get an empty file when trying to edit it using pyhon? [duplicate]

Time:09-30

I am trying to edit a text file that is html using python. When printing it, it gives an empty file. Why it gets empty? I tried to print it because I don't know how to return it. Here's the code:

    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)

CodePudding user response:

It becomes empty because you open file for writing with 'w' and thus make it empty as per documentation - just change it to 'r' or 'a'

CodePudding user response:

It would remain empty because you're just creating a file & not writing anything on it.

  • Related