Skip to content

Commit 26233e3

Browse files
Merge pull request #801 from akash720/master
Add random file mover
2 parents 3ce49cc + 6c5ad96 commit 26233e3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

random_file_move.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Script Name : random_file_move.py
2+
# Author(s) : Akash Jain
3+
# Created : 1 September 2020
4+
# Last Modified : 1 September 2020
5+
# Version : 1.0
6+
# Description : This will move specified number of files(given in ratio) from the src directory to dest directory.
7+
8+
9+
import os, random
10+
import argparse
11+
12+
def check_ratio(x):
13+
try:
14+
x = float(x)
15+
except ValueError:
16+
raise argparse.ArgumentTypeError("%r not a floating-point literal" % (x,))
17+
18+
if (x < 0.0 or x > 1.0):
19+
raise argparse.ArgumentTypeError("%r not in range [0.0, 1.0]" % (x))
20+
return x
21+
22+
desc = 'Script to move specified number of files(given in ratio) from the src directory to dest directory.'
23+
usage = 'python random_file_move.py -src [SRC] -dest [DEST] -ratio [RATIO]'
24+
25+
parser = argparse.ArgumentParser(usage = usage, description = desc)
26+
parser.add_argument('-src', '--src', type = str, required = True,
27+
help = '(REQUIRED) Path to directory from which we cut files. Space not allowed in path.')
28+
parser.add_argument('-dest', '--dest', type = str, required = True,
29+
help = '(REQUIRED) Path to directory to which we move files. Space not allowed in path.')
30+
parser.add_argument('-ratio', '--ratio', type = check_ratio, required = True,
31+
help = "(REQUIRED) Ratio of files in 'src' and 'dest' directory.")
32+
33+
args = parser.parse_args()
34+
35+
src = args.src
36+
dest = args.dest
37+
ratio = args.ratio
38+
39+
files = os.listdir(src)
40+
size = int(ratio * len(files))
41+
42+
print('Move {} files from {} to {} ? [y/n]'.format(size, src, dest))
43+
if input().lower() == 'y':
44+
for f in random.sample(files, size):
45+
try:
46+
os.rename(os.path.join(src, f), os.path.join(dest, f))
47+
except Exception as e:
48+
print(e)
49+
print('Successful')
50+
else:
51+
print('Cancelled')

0 commit comments

Comments
 (0)