Skip to content

Commit c580c47

Browse files
committed
add WebSpiderSeed/io
1 parent 1f443de commit c580c47

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Author: herongwei
3+
Date: 2021-06-26 11:20:16
4+
LastEditTime: 2021-06-26 11:26:27
5+
LastEditors: Please set LastEditors
6+
Description: IO 文件操作/帮助类
7+
FilePath: /WebSpiderSeed/lib/io_helper.py
8+
'''
9+
#!/usr/bin/spython
10+
# -*- coding: utf-8 -*-
11+
import os
12+
13+
class IOHelper(object):
14+
@staticmethod
15+
def exist(path):
16+
return os.path.exist(path)
17+
18+
@staticmethod
19+
def read_all(path):
20+
file_obj = open(path)
21+
try:
22+
file_context = file_obj.read()
23+
finally:
24+
file_obj.close()
25+
return file_context
26+
27+
@staticmethod
28+
def write_all(path, data):
29+
IOHelper.delfile(path)
30+
with open(path, 'w') as file_object:
31+
file_object.write(data)
32+
33+
@staticmethod
34+
def write_line(path, data_arr):
35+
with open(path, 'a+') as file_object:
36+
for line in data_arr:
37+
file_object.write(line)
38+
file_object.write("\n")
39+
40+
@staticmethod
41+
def delfile(path):
42+
if os.path.exists(path): # 如果文件存在
43+
os.remove(path)
44+
45+
46+

0 commit comments

Comments
 (0)