python 获得任意路径下的文件及其根目录的方法
作者:BroStayHungry 时间:2022-02-02 17:32:15
似乎有一段时间没有更新博客了,这里就写点小功能,轻松获得电脑任意路径下的文件及文件夹,并将其写入word,以下是主要代码:
**import os**
**from os import walk**
# 获取文件夹的中的文件夹和文件夹里文件
def do_file(save_filepath,o_filepath): #定义函数 传入写入文档保存的位置和要操作的任意电脑路劲
file=open(save_filepath,"w+")
# 遍历文件路径
for parent,dirnames,filenames in walk(o_filepath):
file.write(("根目录为:{0}\n\n").format(parent))
for dirname in dirnames:
file.write((" 里面的文件夹有:{0}\n\n").format(dirname))
for filename in filenames:
file.write((" 里面的文件有:{0}\n\n").format(filename))
file.close()
do_file(" "," ")#传入相关的参数即可
这里还可以把函数封装以及获得每个文件的文件路径 方法一样的 因此没写 大家可以试试
来源:https://blog.csdn.net/qq_25279303/article/details/80295827
标签:python,路径,文件,根目录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于xmlhttp乱码的解决方法
2008-09-24 17:20:00
pandas实现导出数据的四种方式
2023-01-03 14:57:57
Python Selenium操作Cookie的实例方法
2022-10-22 10:21:34
总结python 三种常见的内存泄漏场景
2023-02-18 16:37:49
Python标准库06之子进程 (subprocess包) 详解
2021-05-24 02:00:25
编写SQL Server的扩展存储过程实例
2009-01-20 15:45:00
学生如何免费使用Pycharm专业版学生认证教程
2021-11-20 16:25:13
![](https://img.aspxhome.com/file/2023/7/95587_0s.jpg)
python列表插入append(), extend(), insert()用法详解
2021-05-12 13:32:40
![](https://img.aspxhome.com/file/2023/4/93874_0s.png)
Python实现二叉树的最小深度的两种方法
2022-05-24 03:30:17
win2000server IIS和tomcat5多站点配置
2023-06-26 04:45:33
MSSQL中部分字段重复数据的删除方法
2008-12-29 13:59:00
Python getattr()函数使用方法代码实例
2022-03-04 03:30:51
Python文本相似性计算之编辑距离详解
2022-04-28 12:14:23
![](https://img.aspxhome.com/file/2023/8/86308_0s.png)
Python Django实现layui风格+django分页功能的例子
2023-10-07 00:04:28
![](https://img.aspxhome.com/file/2023/1/72101_0s.jpg)
XML与HTML的结合(下)
2008-09-05 17:20:00
解决windows下python3使用multiprocessing.Pool出现的问题
2021-11-03 23:34:37
![](https://img.aspxhome.com/file/2023/3/109573_0s.jpg)
单步调试 step into/step out/step over 区别说明
2022-03-09 20:03:26
![](https://img.aspxhome.com/file/2023/9/115629_0s.jpg)
如何实现对整个站点所有页面的操作?
2010-05-19 21:20:00
Quickwork For Asp -实战之后台管理
2009-12-31 19:13:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2010111171421531s.jpg)
Python快速实现一键抠图功能的全过程
2021-03-03 14:58:39
![](https://img.aspxhome.com/file/2023/6/66926_0s.png)