-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyFiles.py
More file actions
53 lines (44 loc) · 1.68 KB
/
copyFiles.py
File metadata and controls
53 lines (44 loc) · 1.68 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
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enter two folder paths a and b, and copy files in a to b,
# count the number of copied files. igonore the duplicate
import sys
import os
import shutil
def copyFiles(src, dst):
srcFiles = os.listdir(src)
dstFiles = dict(map(lambda x:[x, ''], os.listdir(dst)))
filesCopiedNum = 0
for file in srcFiles:
src_path = os.path.join(src, file)
dst_path = os.path.join(dst, file)
# copy the folder. if exist, recursive call this function,
# otherwise create a new folder then call this function
if os.path.isdir(src_path):
if not os.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum += copyFiles(src_path, dst_path)
# copy the file. if exist, do nothing
elif os.path.isfile(src_path):
if not dstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum += 1
return filesCopiedNum
def test():
src_dir = os.path.abspath(raw_input('Please enter the source path: '))
if not os.path.isdir(src_dir):
print 'Error: source folder does not exist!'
return 0
dst_dir = os.path.abspath(raw_input('Please enter the destination path: '))
if os.path.isdir(dst_dir):
num = copyFiles(src_dir, dst_dir)
else:
print 'Destination folder does not exist, a new one will be created.'
os.makedirs(dst_dir)
num = copyFiles(src_dir, dst_dir)
print 'Copy complete:', num, 'files copied.'
if __name__ == '__main__':
if len(sys.argv) > 1:
copyFiles(sys.argv[1], sys.argv[2])
else:
test()