File handling -write

I have a query. whether this read and write function works for files with .txt extensions only?
Beacuse i tried using the same syntax with .docx file and its showing error.
Kindly help me understand this

Hi, please refer to this post.