今天我们继续5分钟学系列,通过简短的代码,来完成实用的功能。今天我们来实现一个简易增强版的 Everything。为什么简易呢,因为单从搜索的功能上来说,肯定没有成熟的 Everything 来的强大,但是基本搜索还是可以的;而对于增强呢,就是我们可以加入自己常用的操作,比如直接把搜索到的文件拷贝的指定目录下,这样是不是又节省了很多时间呢。
这里我们首先想到的就是 Python 内置库 os 的 walk 函数,该函数可以获取到指定目录下的文件和子文件,它返回三个属性,分别是父目录、所包含的子目录和所包含的文件
./
['.idea', 'doc', 'Flask Web', 'Flask-Web', 'html', 'jpg', 'md', 'pdf', 'xml']
['.DS_Store', '1.jpg', '2.xml', '3.pdf', 'personal document.rar', 'search_all.py', 'test.py']
=========
./jpg
[]
['1.jpg', '2.xml']
有了这么一个方便的函数,我们就可以实现遍历指定文件夹下的所有文件夹,找出我们需要的文件了
for dirpath, dirname, files in os.walk('./'):
if file_name in files:
print(dirpath)
print(dirname)
print(files)
cpath = os.path.join(dirpath, file_name)
print(cpath)
当然了,当前我们还只是写死了要搜索的目录,为了实现搜索任意指定的文件夹功能,我们来封装成函数,把搜索目录作为参数传进来
def search_all(spath, sfile):
for dirpath, dirname, files in os.walk(spath):
if sfile in files:
print(dirpath)
print(dirname)
print(files)
cpath = os.path.join(dirpath, file_name)
print(cpath)
print("=========")
下面我们再进行拷贝的操作
拷贝文件,可以使用 shutil 库的 copy 函数,只需要传入源文件和目的地址就可以,我们把相关代码加入
def search_all(spath, sfile, dpath):
for dirpath, dirname, files in os.walk(spath):
if sfile in files:
print(dirpath)
print(dirname)
print(files)
cpath = os.path.join(dirpath, file_name)
print(cpath)
shutil.copy(cpath, os.path.join(dpath, file_name))
print("=========")
这样写看起来似乎很美好,但是存在两个问题
下面我们就来逐个解决这两个问题
首先解决目的地址不存在的问题,我们来做个判断,对于不存在的目的地址,先创建目录,再进行文件拷贝
def search_all(spath, sfile, dpath):
for dirpath, dirname, files in os.walk(spath):
if sfile in files:
print(dirpath)
print(dirname)
print(files)
cpath = os.path.join(dirpath, file_name)
print(cpath)
if not os.path.exists(dpath):
os.mkdir(dpath)
shutil.copy(cpath, os.path.join(dpath, file_name))
print("=========")
通过 os 库的 exists 函数来判断文件夹是否存在,如果不存在,再通过 mkdir 函数来创建对应目录
下面我们再来思考下,如何解决多个文件的问题呢,因为不同目录下的文件名可能相同,但是它们很有可能不是一样的文件,所以我们不能直接覆盖,还是需要做一定的区分的。
我这里的处理方式就是如果存在多个需要拷贝的文件,就在指定的目的文件夹下创建包含目录信息的文件夹,并把对应的文件拷贝进去,这样就可以很好的区分文件的来源了
def search_all(spath, sfile, dpath):
file_num = 0
file_path = []
for dirpath, dirname, files in os.walk(spath):
if sfile in files:
print(dirpath)
print(dirname)
print(files)
cpath = os.path.join(dirpath, file_name)
print(cpath)
print("=======")
file_path.append(cpath)
file_num += 1
if not os.path.exists(dpath):
os.mkdir(dpath)
if file_num == 1:
shutil.copy(file_path[0], os.path.join(dpath, file_name))
else:
for file in file_path:
mk_path = file.replace(r'/', '-')
mpath = os.path.join(dpath, mk_path)
os.mkdir(mpath)
shutil.copy(file_path[0], os.path.join(mpath, file_name))
这样,我们就基本完成了一个简易的增强型 Everything 工具,快来试试吧!