|
|
@@ -1,5 +1,6 @@
|
|
|
import os
|
|
|
from magic_pdf.io.AbsReaderWriter import AbsReaderWriter
|
|
|
+from loguru import logger
|
|
|
class DiskReaderWriter(AbsReaderWriter):
|
|
|
def __init__(self, parent_path, encoding='utf-8'):
|
|
|
self.path = parent_path
|
|
|
@@ -7,8 +8,8 @@ class DiskReaderWriter(AbsReaderWriter):
|
|
|
|
|
|
def read(self, mode="text"):
|
|
|
if not os.path.exists(self.path):
|
|
|
- print(f"文件 {self.path} 不存在")
|
|
|
- return None
|
|
|
+ logger.error(f"文件 {self.path} 不存在")
|
|
|
+ raise Exception(f"文件 {self.path} 不存在")
|
|
|
if mode == "text":
|
|
|
with open(self.path, 'r', encoding = self.encoding) as f:
|
|
|
return f.read()
|
|
|
@@ -22,11 +23,12 @@ class DiskReaderWriter(AbsReaderWriter):
|
|
|
if mode == "text":
|
|
|
with open(self.path, 'w', encoding=self.encoding) as f:
|
|
|
f.write(data)
|
|
|
- print(f"内容已成功写入 {self.path}")
|
|
|
+ logger.info(f"内容已成功写入 {self.path}")
|
|
|
+
|
|
|
elif mode == "binary":
|
|
|
with open(self.path, 'wb') as f:
|
|
|
f.write(data)
|
|
|
- print(f"内容已成功写入 {self.path}")
|
|
|
+ logger.info(f"内容已成功写入 {self.path}")
|
|
|
else:
|
|
|
raise ValueError("Invalid mode. Use 'text' or 'binary'.")
|
|
|
|
|
|
@@ -42,6 +44,6 @@ if __name__ == "__main__":
|
|
|
# 从文件读取内容
|
|
|
content = drw.read()
|
|
|
if content:
|
|
|
- print(f"从 {file_path} 读取的内容: {content}")
|
|
|
+ logger.info(f"从 {file_path} 读取的内容: {content}")
|
|
|
|
|
|
|