-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncnames.py
More file actions
47 lines (41 loc) · 1.28 KB
/
Copy pathsyncnames.py
File metadata and controls
47 lines (41 loc) · 1.28 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
from os import listdir,rename,remove
from os.path import isdir,isfile,join,splitext,getsize
from click import echo
import click
@click.command()
@click.option('-f',type=click.STRING, help="Folder path")
@click.option('-nm',type=click.STRING, help="Name")
def sync(f,nm):
"""Synchronizes the names of the folder and its subcontents to the given name"""
if isdir(f):
for file in listFilesInDescBySize(f):
split = splitext(file);
if split[1] in ('.jpg','.txt','.nfo','.com'):
remove(file)
echo('Removed Junk File %s' %file)
else:
newName = join(f,nm+split[1])
echo('Renaming %s to %s' % (file,newName))
try:
rename(file,newName)
except FileExistsError:
echo('File %s reported to be existing. So removing %s' % (newName,file))
remove(file)
echo('Renaming folder %s to %s' %(f,nm))
rename(f,nm)
else:
raise ValueError('This command works only for directorys.')
def listFilesInDescBySize(path):
files = []
for file in listdir(path):
fileName = join(path,file)
if isfile(fileName):
files.append(fileName)
else:
echo('Ignoring %s' %fileName)
for i in range(len(files)):
files[i] = (files[i], getsize(files[i]))
files.sort(key=lambda fileName:fileName[1], reverse=True)
for i in range(len(files)):
files[i] = files[i][0]
return files