python 关于文件夹的操作

在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir()

#  path 表示文件或文件夹的绝对路径

其中os.lister(path)是将此路径内的文件或文件夹的路径列出来

os.path.isdir(path)是判断该路径是否是问价夹

os.path.join(BASE_DIR,name)是用于拼接路劲,BASE_DIR指的是当前文件所在的根目录,name,指的是文件名,

其中BASE_DIR=os.path.dirname(os.path.dirname(__file__))

具体代码如下:

def func(file_path):
    if os.path.isdir(file_path):
    p=os.listdir(file_path)
    for i in range(len(p)):
        res=p[i]
        msg=os.path.join(file_path,res)
        print(msg)
        func(msg)

file_path=input('输入文件夹的路径:')
func(file_path)
View Code

猜你喜欢

转载自www.cnblogs.com/yang220/p/yang_4.html