目录
一、输出目录所在的文件以及文件夹
import os
print(os.getcwd())#获得当前文件的地址
print(os.path.join('MyProjects','AI'))#让Python自动处理路径连接
for item in os.listdir():#列出该文件下的所有文件
print(item, os.path.isdir(item))#循环一遍看谁是文件谁是文件夹
#os.scandir(指定的绝对路径或者相对路径)
for file in os.scandir():
print(file.name, file.is_dir())
二、遍历、搜索文件及查询文件信息
- 字符串A.startswith(字符串B):字符串A是否以字符串B开头
- 字符串A.endswith(字符串B):字符串A是否以字符串B结尾
- os.walk(指定的绝对路径或者相对路径)
- dripath是文件路径
- files是dirpath这个文件夹里的文件列表
- print(glob.glob('*.py'))#文件搜索匹配
- print(fnmatch.fnmatch('makerbean','m*an'))#匹配文件名
import os
import glob
import fnmatch
import datetime
'''
第一、
#dirnames是dirpath这个文件夹下的子文件列表
for dirpath, dirnames, files in os.walk('./'):#把文件夹里的文件夹里的文件夹的文件都找出来
print(f'发现文件夹{dirpath}')#dripath是文件路径
print(files)#files是dirpath这个文件夹里的文件列表
'''
'''
第二、
#字符串A.startswith(字符串B):字符串A是否以字符串B开头
#字符串A.endswith(字符串B):字符串A是否以字符串B结尾
print('makerbean'.startswith('abc'))
print('makerbean'