-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomiser
executable file
·51 lines (42 loc) · 1.34 KB
/
randomiser
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
#!/usr/bin/env python3
import os
import random
import sys
from FileClass import File
# Length of generated filenames, in characters
FILENAMELENGTH = 8
# To help pylint with scoping
def main():
if 2 <= len(sys.argv):
if len(sys.argv) == 2 and os.path.isdir(sys.argv[1]):
root = sys.argv[1]
else:
print('''Usage: {0} [directory]
Rename all files in <directory> to have random names but keep extensions.
If not given, <directory> will be the current directory. Will always ignore
files named `{0}` and directories. All renames are checked and retried if files
are not identical.'''.format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
else:
root = '.'
files = list()
for d, _, fs in os.walk(root):
for f in fs:
path = os.path.join(d, f)
if path == sys.argv[0]:
continue
files.append(File(path))
for f in files:
while True:
try:
randnum = random.getrandbits(FILENAMELENGTH * 4)
randname = '{{:0{}x}}'.format(FILENAMELENGTH).format(randnum)
f.SafeSelfCopy(randname)
except FileExistsError:
pass
else:
break # Next file
for f in files:
f.Remove()
if __name__ == '__main__':
main()