python文件检索模拟find命令

#path代表待搜索的目录路径,result存储搜索到的文件路径列表

#函数将path目录中的全部子目录和文件找到保存至result

def search_dir(path,result):

#使用os中的listdir得到path下的目录和文件,保存到child_files

child_files = os.listdir(path)

for child in child_files:

#通过join函数拼接子目录或文件的路径,存储至child

child = os.path.join(path,child)

#child保存至result

if os.path.isdir(child):#如果child是一个子目录

search_dir(child,result)#调用search_dir继续搜索child

#输入搜索目录和doc文件保存的目录

input_dir = input("输入待搜索的目录:")

output_dir = input("输入保存文件的目录:")

#设置保存子目录与文件路径的列表files

files = list()

#将input中的全部子目录和文件路径找到并保存到files

search_dir(input_dir,files)

for file in files:#遍历files

print("find %s"%(file))#打印搜索到的路径

#如果该路径是一个docx文件

if os.path.isfile(file) and file.endswith('.docx'):

print("move %s"%(file))#打印提示信息

shutil.move(file,output_dir)#将文件移到时output_dir

相关推荐
江沉晚呤时5 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
电脑能手5 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
Edward-tan6 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
倔强青铜三6 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试
倔强青铜三7 小时前
苦练Python第17天:你必须掌握的Python内置函数
人工智能·python·面试
迷路爸爸1807 小时前
让 VSCode 调试器像 PyCharm 一样显示 Tensor Shape、变量形状、变量长度、维度信息
ide·vscode·python·pycharm·debug·调试
咸鱼鲸8 小时前
【PyTorch】PyTorch中的数据预处理操作
人工智能·pytorch·python
Dxy12393102168 小时前
Python ExcelWriter详解:从基础到高级的完整指南
开发语言·python
金玉满堂@bj8 小时前
Conda 安装包的用途
python