Python-os-02-文件夹遍历,文件或者文件夹判断
2019-01-30 本文已影响0人
Data_Python_VBA
系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
- 这个系列讲讲os模块常用功能
- 本文介绍:文件夹遍历,文件或者文件夹判断
Part 1:代码
import os
current_address = os.path.dirname(os.path.abspath(__file__))
file_list = os.listdir(current_address)
for file_ in file_list:
file_address = os.path.join(current_address, file_)
if os.path.isfile(file_address):
print("这个是文件,文件名称:", file_address)
elif os.path.isdir(file_address):
print("这个是文件夹,文件夹名称:", file_address)
else:
print("这个情况没遇到")
代码截图

文件夹内部

运行结果

Part 2:部分代码解读
-
os.path.isfile()
判断是否为文件 -
os.path.isdir()
判断是否为文件夹
本文为原创作品,欢迎分享朋友圈
常按图片识别二维码,关注本公众号
Python 优雅 帅气
