python版本:python 2.x(没试过python3.x,可能会报错吧,我是用的Python2.7.5写的)
功能:会将当前目录下的文件夹和文件全部都扫一遍,找到符合要求的格式文件会将该格式的文件复制到指定的目录文件夹里面(里面加了一个判断操作,就是文件中有同名文件的操作。支持中文目录,但不建议使用中文目录,我测过中文目录没出什么问题,但也只是粗略的测了一下,最好还是不要用中文目录吧)
- 发现有重复的文件名:全部覆盖:Y,重命名:N, 覆盖单个:S(这里是支持大小写的)
- Y :就是后续的操作都是直接将同名的文件给覆盖掉
- N :修改同名文件。就是输入一个新的名字来避免同名,这里面可能会出现你输入的这个新的名字在这个文件夹也有出现过。不过问题也不大我也做了操作处理,根据提示来就好了
- S:这个意思就是,只是将当前的这个文件进行覆盖掉,后面如果再出现同名的文件,还是会暂停复制的操作继续出现上述的提示操作
操作:创建一个文本文件,复制代码内容到这个文本文件里面,结束之后将文件名的后缀由 .txt 改为 .py 。然后双击该文件即可运行
代码逻辑:
#!/usr/bin/python
# coding=utf-8
import shutil,os
isCover = False
newName = ""
print(u"请输入文件名(默认创建文件夹为:img) - 不建议使用中文命名")
createfile = raw_input("please enter:")
createfile = createfile.decode('gbk').encode('gbk')
#判断是否是路径
def isPath(path):
for singleStr in path:
if singleStr == "/":
print(u"路径格式不对:发现字符 '/' ,应该是 '\\' 。请重新输入:")
path = raw_input("please enter:")
break
return path
#路径初始化
def pathInit(path):
if path == "":
path = ".\\img"
else:
findS = "\\" in path
if not findS:
path = ".\\" + path
return path
#重名文件操作
def repeatNameHandel(isSelectStr, oldName, file_list):
global isCover
global newName
if isSelectStr == "Y" or isSelectStr == "y":
isCover = True
return ""
elif isSelectStr == "N" or isSelectStr == "n":
print("\nold name: "+oldName+" ,Please enter a changed file name:")
newName = raw_input("please enter:")
newName = newName.decode('gbk').encode('gbk')
if oldName == newName:
print(u"\n新文件名与旧文件名重复,请重新输入...")
repeatNameHandel("N", oldName, file_list)
else:
splitList_old = os.path.splitext(oldName)
splitList_new = os.path.splitext(newName)
if splitList_new[1] == "" or splitList_new[1] == "." or splitList_old[1] != splitList_new[1]:
print(u"\n当前文件名的后缀输入有误,请重新输入...")
print("input filename: " + newName)
print("file suffix required: " + splitList_old[1])
repeatNameHandel("N", oldName, file_list)
for _file_name in file_list:
if _file_name == newName:
print(u"\n当前文件内已存在该文件名,请重新输入...")
print("file exists: " + newName)
repeatNameHandel("N", oldName, file_list)
return newName
else:
print(u"\n输入有误,请重新输入:覆盖:Y,重命名:N")
isSelectStr = raw_input("please enter:")
repeatNameHandel(isSelectStr, oldName, file_list)
#复制图片
def copyImg(_createfile):
global isCover
fileNum = 0
g = os.walk(".", topdown=True)
for path,dir_list,file_list in g:
for file_name in file_list:
if path == _createfile:
continue
textList = os.path.splitext(file_name)
if(textList[1] == ".jpg" or textList[1] == ".png"):
pathName = os.path.join(path, file_name)
pathState = os.path.exists(_createfile)
if pathState == False:
os.mkdir(_createfile)
_newName = ""
if not isCover:
imgfile = os.walk(_createfile, topdown=False)
for pathImg,dir_listImg,file_listImg in imgfile:
for _file_name in file_listImg:
if _file_name == file_name:
print(u"\n发现有重复的文件名:全部覆盖:Y,重命名:N, 覆盖单个:S")
pathName = pathName.decode('gbk').encode('gbk')
print("repeat file : "+pathName)
isSelectStr = raw_input("please enter:")
if isSelectStr == "s" or isSelectStr == "S":
continue
_file_name = _file_name.decode('gbk').encode('gbk')
newName = repeatNameHandel(isSelectStr, _file_name, file_listImg)
if newName != "" and newName != None:
print("\nName changed successfully: "+_file_name+" To "+newName)
_newName = newName
newName = ""
continue
if _newName == "":
_newName = file_name
pathName2 = os.path.join(_createfile, _newName)
if pathName != pathName2:
fileNum = fileNum + 1
pathName = pathName.decode('gbk').encode('gbk')
pathName2 = pathName2.decode('gbk').encode('gbk')
print("copy:" + pathName + " To " + pathName2)
shutil.copy(pathName, pathName2)
endStr = "已成功将文件拷贝到:"
endStr = endStr.decode('utf-8').encode('gbk')
_createfile = _createfile.decode('gbk').encode('gbk')
print(("\n"+endStr+_createfile+" , Total number of copied files: %d"+"\n")%(fileNum))
createfile = isPath(createfile)
createfile = pathInit(createfile)
copyImg(createfile)
os.system("pause")
复制整个文件夹
#!/usr/bin/python
# coding=utf-8
import os
import shutil
target_path = os.path.abspath('./qiu')
source_path = os.path.abspath('./ss/qiu')
print(target_path,source_path)
if not os.path.exists(target_path):
os.makedirs(target_path)
if os.path.exists(source_path):
shutil.rmtree(source_path)
shutil.copytree(target_path, source_path)
print('copy dir finished!')
os.system("pause")