- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyFiles.py
More file actions
Latest commit
53 lines (44 loc) · 1.68 KB
/
Copy pathcopyFiles.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
importsys
importos
importshutil
defcopyFiles(src, dst):
srcFiles=os.listdir(src)
dstFiles=dict(map(lambdax:[x, ''], os.listdir(dst)))
filesCopiedNum=0
forfileinsrcFiles:
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
ifos.path.isdir(src_path):
ifnotos.path.isdir(dst_path):
os.makedirs(dst_path)
filesCopiedNum+=copyFiles(src_path, dst_path)
# copy the file. if exist, do nothing
elifos.path.isfile(src_path):
ifnotdstFiles.has_key(file):
shutil.copyfile(src_path, dst_path)
filesCopiedNum+=1
returnfilesCopiedNum
deftest():
src_dir=os.path.abspath(raw_input('Please enter the source path: '))
ifnotos.path.isdir(src_dir):
print'Error: source folder does not exist!'
return0
dst_dir=os.path.abspath(raw_input('Please enter the destination path: '))
ifos.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__':
iflen(sys.argv) >1:
copyFiles(sys.argv[1], sys.argv[2])
else:
test()