-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_handle.py
85 lines (69 loc) · 2.83 KB
/
file_handle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
from pathlib import Path
"""
xjiebot 文件操作
懒的写直接复制
获取自身路径下的文件
"""
def file_path(file):
return Path(__file__).resolve().parent / file
class xj_file_handle:
def __init__(self):
pass
def xj_file_reading(self, file_name: str, file_content: str = None):
json_file_path_reading = file_path(file_name)
try:
with json_file_path_reading.open("r", encoding="utf-8") as json_file:
loaded_data = json.load(json_file)
if file_content is None:
return loaded_data
return loaded_data.get(file_content, None)
except FileNotFoundError:
print(f"File not found: {file_name}")
except json.JSONDecodeError:
print(f"Error decoding JSON from the file: {file_name}")
except Exception as e:
print(f"An error occurred: {e}")
def xj_file_change(self, file_name: str, file_key: str, file_content: str):
json_file_path_change = file_path(file_name)
try:
with json_file_path_change.open("r", encoding="utf-8") as json_file:
loaded_data = json.load(json_file)
except FileNotFoundError:
print(f"文件 {file_name} 未找到。")
return
except json.JSONDecodeError:
print(f"{file_name} 文件内容不是有效的JSON格式。")
return
if file_key not in loaded_data:
print(f"键 '{file_key}' 在文件中不存在。")
return
loaded_data[file_key] = file_content
try:
with json_file_path_change.open("w", encoding="utf-8") as json_file:
json.dump(loaded_data, json_file, indent=4)
except IOError as e:
print(f"写入文件时发生错误: {e}")
def get_keys_ending_with_key(self, json_data, key_suffix='_KEY'):
try:
json_file_path_reading = file_path(json_data)
with open(json_file_path_reading, "r", encoding="utf-8") as json_file:
loaded_data = json.load(json_file)
except FileNotFoundError:
print(f"Error: The file {json_file_path_reading} was not found.")
return None
except json.JSONDecodeError:
print(f"Error: Failed to decode JSON from file {json_file_path_reading}.")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
return None
result = {}
for key in loaded_data.keys():
if key.endswith(key_suffix) and loaded_data[key]:
result[key] = loaded_data[key]
return result
def read_filenames_with_pathlib(directory):
path_obj = Path(directory)
filenames = [file.name for file in path_obj.iterdir() if file.is_file()]
return filenames