-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileClass.py
63 lines (51 loc) · 2.07 KB
/
FileClass.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
#!/usr/bin/env python3
import hashlib
import os
import shutil
class File:
'''A class reprensenting a file.
Exposed members:
- dir: str, the directory of the file,
- file: str, the name and extension of the file,
- hash: str, the hexadecimal MD5 hash of the file.
hash is guaranteed to stay constant throughout renamings and copies.'''
@staticmethod
def GetHash(path):
'''Get the hexadecimal MD5 hash of the file.'''
with open(path, 'rb') as f:
return hashlib.md5(f.read()).hexdigest()
def __init__(self, path):
self.dir, self.file = os.path.split(path)
self.hash = File.GetHash(path)
def SafeCopy(self, topath):
'''Copy this file to another location. Retry if hashes do not match.'''
frompath = os.path.join(self.dir, self.file)
if frompath == topath:
return
print("SafeCopy:", frompath, '→', topath)
if os.path.isfile(topath):
raise FileExistsError(topath)
shutil.copy2(frompath, topath)
tohash = File.GetHash(topath)
while self.hash != tohash:
print("SafeCopy:", frompath, '→', topath)
shutil.copy2(frompath, topath)
tohash = File.GetHash(topath)
def SafeMove(self, topath):
'''SafeCopy this file then delete original. This object is altered to
represent the new file.'''
self.SafeCopy(topath)
self.Remove()
self.dir, self.file = os.path.split(topath)
def SafeRename(self, toname):
'''SafeMove this file to a new name while keeping extension. This object
is altered to represent the new file.'''
_, ext = os.path.splitext(self.file)
self.SafeMove(os.path.join(self.dir, toname + ext))
def SafeSelfCopy(self, toname):
'''SafeCopy this file to a new name while keeping extension.'''
_, ext = os.path.splitext(self.file)
self.SafeCopy(os.path.join(self.dir, toname + ext))
def Remove(self):
'''Remove that file.'''
os.remove(os.path.join(self.dir, self.file))