Skip to content

Commit ca4ea30

Browse files
committed
option for directory whose files to classify
1 parent 004877c commit ca4ea30

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

classifier/classifier.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ def moveto(file, from_folder, to_folder):
2828
os.rename(from_file, to_file)
2929

3030

31-
def classify(formats, output):
31+
def classify(formats, output, directory):
3232
print("Scanning Files")
3333

34-
directory = getcwd()
35-
3634
for file in os.listdir(directory):
3735
filename, file_ext = os.path.splitext(file)
3836
file_ext = file_ext.lower()
@@ -49,15 +47,15 @@ def classify(formats, output):
4947
print("Done!")
5048

5149

52-
def classify_by_date(date_format, output_dir):
50+
def classify_by_date(date_format, output, directory):
5351
print("Scanning Files")
5452

55-
directory = getcwd()
5653
files = [x for x in os.listdir(directory) if not x.startswith('.')]
57-
creation_dates = map(lambda x: (x, arrow.get(os.path.getctime(x))), files)
54+
creation_dates = map(lambda x: (x, arrow.get(os.path.getctime(os.path.join(directory, x)))), files)
5855

5956
for file, creation_date in creation_dates:
6057
folder = creation_date.format(date_format)
58+
folder = os.path.join(output, folder)
6159
moveto(file, directory, folder)
6260

6361
print("Done!")
@@ -88,6 +86,9 @@ def main():
8886
parser.add_argument("-o", "--output", type=str,
8987
help="Main directory to put organized folders")
9088

89+
parser.add_argument("-d", "--directory", type=str,
90+
help="The directory whose files to classify")
91+
9192
parser.add_argument("-dt", "--date", action='store_true',
9293
help="Organize files by creation date")
9394

@@ -120,9 +121,18 @@ def main():
120121
else:
121122
output = _format_arg(args.output)
122123

124+
if args.directory is None:
125+
directory = getcwd()
126+
else:
127+
directory = _format_arg(args.directory)
128+
if args.output is None:
129+
''' if -d arg given without the -o arg, keeping the files of -d
130+
in the -d path only after classifying '''
131+
output = directory
132+
123133
if args.date:
124-
classify_by_date('DD-MM-YYYY', output)
134+
classify_by_date('DD-MM-YYYY', output, directory)
125135
else:
126-
classify(formats, output)
136+
classify(formats, output, directory)
127137

128138
sys.exit()

0 commit comments

Comments
 (0)