-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_op.py
More file actions
63 lines (46 loc) · 1.19 KB
/
Copy pathfile_op.py
File metadata and controls
63 lines (46 loc) · 1.19 KB
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
# file operation
import os
import sys
def file_walk(dir):
for root, dirs, files in os.walk(dir):
print(root) # current root
print(dirs) # under root all dirs
print(files) # under root all files
def file_list(dir):
for file in os.listdir(dir):
print(file) # list under dir
import shutil
# copy file
def copy_file(source, dest):
shutil.copy(source, dest)
# ---------------------- file store ------------------------ #
# json
import json
def json_read(path='/name.json'):
with open(path, 'r') as f:
jsobj = json.loads(f.read())
# 另外一种方式
jsobj = json.load(f)
def json_save(path="/path/name.json"):
dic = {}
with open(path, 'w') as f:
jsobj = json.dumps(dic, sort_keys=True, indent=4, separators=(',', ':'))
f.write(jsobj)
f.close()
# 另外个一种方式
json.dump(dic, f)
# xml
def xml_read(path='/name.xml'):
pass
# txt
# csv
#######
import csv
def read():
reader = csv.reader(open("path", encoding='utf-8'))
for row in reader:
print(row)
def write():
out = open(path, 'w')
writer = csv.writer(out)
writer.writerow(["1", "2"])