-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmove_by_ext.py
executable file
·128 lines (111 loc) · 5.12 KB
/
move_by_ext.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python2
# -*- coding: utf8 -*-
"""
move_by_ext.py: Move (or copy/remove) files by extension.
"""
#==============================================================================
# This script find files by a given extension into a directory hierarchy and
# then move (or copy/remove) all of them to the given destination path.
#
# It pretends to be an alternative to these UNIX equivalent commands (but also
# works on windows and with various extensions at same time):
#
# find ./SRC -type f -name *.EXT -exec mv {} ./DEST \;
#
# find ./SRC -type f -name *.EXT -exec cp {} ./DEST \;
#
# find ./SRC -type f -name *.EXT -exec rm -f {} \;
#
#==============================================================================
#==============================================================================
# Copyright 2011 joe di castro <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#==============================================================================
__author__ = "joe di castro <[email protected]>"
__license__ = "GNU General Public License version 3"
__date__ = "26/04/2011"
__version__ = "0.1"
try:
import sys
import os
import shutil
from argparse import ArgumentParser
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def arguments():
"""Defines the command line arguments for the script."""
cur_dir = os.path.curdir
main_desc = """Move (or copy/remove) all files selected by extension into a
directory tree to a destination directory."""
usage = "%(prog)s ext [-s SRC] [-d DST] [-c | -r] [--help]"
parser = ArgumentParser(description=main_desc, usage=usage)
parser.add_argument("ext", nargs='+', help="the extension(s) of the files "
"to process. To use more than one extension, separate "
"them with a space")
parser.add_argument("-s", "--src", dest="src", default=cur_dir, help="the "
"source path. Current dir if none is provided")
parser.add_argument("-d", "--dst", dest="dst", default=cur_dir, help="the "
"destination path. Current dir if none is provided")
group = parser.add_mutually_exclusive_group()
group.add_argument("-c", "--copy", dest="cp", action="store_true",
help="copy all the files with the given extension(s) "
"to the destination directory.")
group.add_argument("-r", "--remove", dest="rm", action="store_true",
help="remove all the files with the given extension(s)."
" Use with caution! remove also in the subdirectories")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s {0}".format(__version__),
help="show program's version number and exit")
return parser
def find_and_process(args, count=0, log=""):
"""Find the files by file extension and process (move/copy/remove) them."""
def process(the_path, the_file):
"""Process each file."""
processed = 0
src_file = os.path.join(the_path, the_file)
dst_file = os.path.join(args.dst, the_file)
if args.rm:
os.remove(src_file)
processed = 1
else:
if not os.path.exists(dst_file): # not replace if already exists
if args.cp:
shutil.copy2(src_file, dst_file)
else:
shutil.move(src_file, dst_file)
processed = 1
return processed
if not os.path.exists(args.dst):
os.mkdir(args.dst)
for path, directories, files in os.walk(args.src):
for fil in files:
# ignore files without extension, can have the same name as the ext
file_ext = fil.split('.')[-1] if len(fil.split('.')) > 1 else None
# ignore dots in given extensions
extensions = [ext.replace('.', '') for ext in args.ext]
if file_ext in extensions:
count += process(path, fil)
opt = int("{0}{1}".format(int(args.rm), int(args.cp)), 2)
log = "Files {0}: {1}".format({0: "moved", 1: "copied", 2: "removed"}[opt],
count)
return log
def main():
"""Main section"""
print(find_and_process(arguments().parse_args()))
if __name__ == "__main__":
main()