-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv_to_dir_by_site.py
48 lines (43 loc) · 1.55 KB
/
mv_to_dir_by_site.py
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
"""
Author: lt
Date: 2019-06-26
Description: 根据域名列表对应的hostname移动对应文件夹,
如从文件中读取到域名http://www.a.com和http://www.b.com,
那么就从source文件夹中寻找名为www.a.com和www.b.com的文件夹移动到目标目录,
注意当文件名冲突时会覆盖
"""
import sys
sys.path.append('./..')
import fire
import os
import shutil
from common import get_host_from_url
def main(sites_p, source_d, output_d):
with open(sites_p, 'r') as fr:
print("Read sites from file: {}".format(sites_p))
all_hosts = []
data = fr.readlines()
for site in data:
site = site.replace('\r', '').replace('\n', '').replace(' ', '')
if site:
try:
host = get_host_from_url(site)
all_hosts.append(host)
except Exception:
print("Get host failed, the site: {}".format(site))
pass
print("All dir to move count: {}".format(len(all_hosts)))
for host in all_hosts:
print("Move dir: {}".format(host))
target_dir = os.path.join(source_d, host)
new_dir = os.path.join(output_d, host)
if os.path.exists(new_dir):
print("The dir already exists, now relace it: {}".format(target_dir))
os.system("rm -rf {}".format(new_dir))
try:
shutil.move(target_dir, output_d)
except Exception as err:
print(err)
print("Move successfully!")
if __name__ == "__main__":
fire.Fire(main)