-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsortCache.py
More file actions
46 lines (37 loc) · 969 Bytes
/
sortCache.py
File metadata and controls
46 lines (37 loc) · 969 Bytes
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
#! /usr/bin/env python3
# rename files to have correct extention based on content
import filetype
import os
import shutil
def formatPath(path):
return os.path.sep.join(path.split("/"))
def mkdir(path):
try:
os.mkdir(formatPath(path))
except FileExistsError:
pass
def cp(src, dst):
src = formatPath(src)
dst = formatPath(dst)
try:
shutil.copyfile(src, dst)
except:
print(f"Error on copying file {src} to {dst}")
def main():
files = os.listdir("./")
mkdir("./output")
fail_path = "./output/fail/"
mkdir(fail_path)
for file in files:
try:
kind = filetype.guess(file)
except IsADirectoryError:
pass
if kind is None:
cp(file,f"{fail_path}/{file}")
else:
dst = f"./output/{kind.extension}"
mkdir(dst)
cp(file, f"{dst}/{file}.{kind.extension}")
if __name__ == '__main__':
main()