forked from tomoncle/Python-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy_file.py
More file actions
74 lines (55 loc) · 1.67 KB
/
study_file.py
File metadata and controls
74 lines (55 loc) · 1.67 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
64
65
66
67
68
69
70
71
72
73
74
# -*- coding=utf-8 -*-
"""
文件操作
"""
import errno
import os
import six
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
else:
print u'文件夹%s 已经存在' % path
def parent_dir(path):
if path[-1] == '/': path = path[0:-1]
return '/'.join(path.split('/')[0:-1])
def del_dir(path):
if not all((os.path.exists(path), os.path.isdir(path))):
return
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(path)
def create_file(name, mode='r', data=""):
try:
parent_path = parent_dir(name)
if parent_path and not os.path.exists(parent_path):
create_dir(parent_path)
with open(name, mode)as f:
f.write(data)
except Exception, e:
print u'%s 创建失败\n异常:%s' % (name, e)
def remove_file(file_path):
try:
os.remove(file_path)
except OSError:
pass
def get_file_size(file_obj):
if (hasattr(file_obj, 'seek') and hasattr(file_obj, 'tell') and
(six.PY2 or six.PY3 and file_obj.seekable())):
try:
curr = file_obj.tell()
file_obj.seek(0, os.SEEK_END)
size = file_obj.tell()
file_obj.seek(curr)
return size
except IOError as e:
if e.errno == errno.ESPIPE:
return
else:
raise
if __name__ == '__main__':
# create_file('/home/aric/pythontest/bb/bbb/abc.txt', 'w', 'hello world')
del_dir('/home/liyuanjun/keys')