class TextView(QtWidgets.QWidget): def set_text(self, file_path): if os.path.isfile(file_path): # if the filepath links to a single file AND this part is right try: with open(file_path, "r", encoding="utf-8") as f: text_content = f.read() self.text.setText(text_content) except Exception as e: print(e) elif os.path.isdir(file_path): ** if the filepath links to a folder** file_list_html = [] file_list = os.listdir(file_path) for file in file_list: file_path_full = os.path.join(file_path, file) if file.endswith(".txt") and os.path.isfile(file_path_full): file_list_html.append(f"<a href='{file_path_full}'>{file}</a>") *this could work but after I clicked the link ,the text would be garbled* # file_list_html.append(file_path_link) file_list_str = "<br>".join(file_list_html) self.text.setOpenExternalLinks(True) self.text.setHtml(file_list_str) self.text.anchorClicked.connect(self.open_link) else: print(f"{file_path} is not a file or directory") def open_link(self, url): file_path = QtCore.QUrl.toLocalFile(url) if file_path.endswith(".txt"): try: # encoding = self.detect_encoding(file_path) # print(encoding) # strange things: this print seems not working , it doesn't show anything in the terminal with open(file_path, "r", encoding="UTF-8") as f: text_content = f.read() self.text.setText(text_content) except Exception as e: print(e) def detect_encoding(file_path): try: with open(file_path, "rb") as f: rawdata = f.read() result = chardet.detect(rawdata) print(result['encoding']) return result['encoding'] except Exception as e: print(e)
showing the list of files in folder:
after i clicked the link:
I used the chardet(the detect_encoding function above) to detect the encoding or i just simply set it to utf-8 but none worked
original txt file is in utf-8: