-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycreatedir
executable file
·64 lines (58 loc) · 2.09 KB
/
pycreatedir
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
#!/usr/bin/python
import subprocess
from optparse import OptionParser
import os
import re
import shutil
def parseCmd():
usage = ""
parser = OptionParser(usage)
parser.add_option("-s", "--simple-rename", dest="is_simple_rename",
action="store_true", default=False,
help="simple rename only");
parser.add_option("-d", "--start-number",action="store", type="int",
dest="start_number",
help="start number/index of filename")
parser.add_option("-m", "--match_str",action="store", type="string",
dest="match_str",
help="only rename matched string")
return parser.parse_args()
## execute the bash command
#def execBash(cmd):
# print cmd
# p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
# out = p.stdout.read()
# #print out
# return out
def execBash(cmd, is_suppress=False, is_print_cmd=False):
if is_print_cmd:
print cmd
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
#(stdoutdata, stderrdata) = p.communicate()
stdoutdata = p.communicate()[0]
p.poll()
if (p.returncode != 0) and (not is_suppress):
print ("\033[1;31mWarning!! command (%s) not ended properly."
"exit status = %d\033[0m" %(cmd, p.returncode))
#out = p.stdout.read()
#print out
return (stdoutdata, p.returncode)
def createDir(start, end):
for i in range(start, end):
dirname = "%04d" % i
print dirname
if not os.path.exists(dirname):
os.makedirs(dirname)
reg_obj=re.compile(r".*%s.?.jpg" % (dirname, ))
# look for corresponding files and move into correct folder
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
result = reg_obj.match(f)
if result:
print ("file match %s " % (f,))
shutil.move(f, dirname)
## if standalone, i.e. called directly from shell
if __name__ == '__main__':
(options, args) = parseCmd()
createDir(int(args[0]), int(args[1]))
#vim: filetype=python