#python
import os
from pickle import FALSE, TRUE
from secrets import choice
import shutil
import os
import re
currentPath = os.getcwd().replace('\\','/') # 获取当前路径
os.chdir(str(currentPath+"/default"))
currentPath = os.getcwd().replace('\\','/') # 获取当前路径
dataNames = os.listdir(currentPath)
# 移动图片函数
def GetWebClipper():
global currentPath
mdName = []
mdPath = []
picName = []
picPath = "/webClipper/网页笔记/assets/"
pagePath = "/webClipper/网页笔记/"
for root,dirs,files in os.walk(currentPath):
for fileObj in files:
name =str(fileObj)
if(".md" in name):
if(name in mdName):
print("存在重复文件:")
print(name)
return
else:
mdName.append(name)
path = os.path.join(root,fileObj).replace('\\','/')
mdPath.append(path)
file = open(path,'r',encoding= "utf-8")
fileList = file.readlines()
for txt in fileList:
if("assets/" in txt):
list = re.findall(r'[(](.*?)[)]', txt) #取括号内字符,返回队列
for i in list:
if("assets/" in i):
sec = i.replace("assets/","").split(".")[0]
picName.append(sec)
file.close()
for root,dirs,files in os.walk(currentPath):
for fileObj in files:
name = str(fileObj)
nameList = name.split(".")
if(nameList[0] in picName):
path = os.path.join(root,fileObj).replace('\\','/')
mdNewPath = currentPath.replace("/default",picPath) + name.strip().replace(' ','')
shutil.move(path,mdNewPath)
for md in mdPath:
mdList = md.split('/')
name = mdList[len(mdList)-1]
mdNewPath = currentPath.replace("/default",pagePath) + name.strip().replace(' ','')
shutil.move(md,mdNewPath)
for fileObj in dataNames:
pass
shutil.rmtree(fileObj)
# 检测移除多余的图片
os.chdir("../webClipper/网页笔记")
NowpicName = []
currentPath = os.getcwd().replace('\\','/') # 获取当前路径
for root,dirs,files in os.walk(currentPath):
for fileObj in files:
name =str(fileObj)
if(".md" in name):
path = os.path.join(root,fileObj).replace('\\','/')
file = open(path,'r',encoding= "utf-8")
fileList = file.readlines()
for txt in fileList:
if("assets/" in txt):
list = re.findall(r'[(](.*?)[)]', txt) #取括号内字符,返回队列
for i in list:
if("assets/" in i):
sec = i.replace("assets/","").split(".")[0]
NowpicName.append(sec)
file.close()
os.chdir("assets")
currentPath = os.getcwd().replace('\\','/') # 获取当前路径
for root,dirs,files in os.walk(currentPath):
for fileObj in files:
onlyName = fileObj.split('.')[0]
if(not onlyName in NowpicName):
os.remove(currentPath+"/"+fileObj)
GetWebClipper()