• json模块下abspath()查找文件绝对路径与join()路径嫁接获取相对路径的差别


    import os
    n=0
    def fun(file):
        global n
        fli_list=os.listdir(file)
        for i in fli_list:
            new_pa = os.path.join('%s'%file,i)
            if os.path.isdir(new_pa):
                n=fun(os.path.abspath(new_pa))
            else:
                siz = os.path.getsize(new_pa)
                n += siz
        return n
    ret=fun(r'D:py练习神秘海螺goweek319')    
    print(ret)

    文件的绝对路径  os.path.abspath (path) 并非文件真实所在路径

    文件的路径嫁接 os.path.join (path,file_name)  反而可以得出文件本身所在路径

    判断文件是否为文件夹 os.isdir(path) 返回True/False

    判断文件是否为文件 os.isfile 同上

    列出文件夹下文件目录 os.listdir(path)

    import os
    def get_size(path):
        n = [path]
        size = 0
        while n:
            path=n.pop( )
            lst=os.listdir(path)
            for name in lst:
                son_path=os.path.join(path,name)
                if os.path.isfile(son_path):
                    size+=os.path.getsize(son_path)
                else:
                    n.append(son_path)
        return size
    print(get_size(r'D:py练习'))
    #使用join(path,file_name)路径嫁接
    import os
    def get_size(path):
        n = [path]
        size = 0
        while n:
            path=n.pop( )
            lst=os.listdir(path)
            for name in lst:
                son_path=os.path.abspath(name)
                if os.path.isfile(son_path):
                    size+=os.path.getsize(son_path)
                else:
                    n.append(son_path)
        return size
    print(get_size(r'D:py练习'))
    #使用abspath(file_name)

    abspath(file_name)的结果如下:

      FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'D:\py练习\神秘海螺\go\week\3\26\金山打字'

    然而我磁盘中金山打字的正确位置:

      D:py练习金山打字

    此代码所属py文件所在位置:

      D:py练习神秘海螺goweek3261.py

     

  • 相关阅读:
    Avoiding the Backup of Online Redo Logs
    RMAN-20201: datafile not found in the recovery catalog
    ORA-15081: failed to submit an I/O operation to a disk
    字符串替换数字问题
    jstl换行符处理
    字符串匹配问题
    careercup题目20131013
    careercup题目20131010
    careercup题目201330928
    面试题(一)
  • 原文地址:https://www.cnblogs.com/lttlpp61007188/p/10575013.html
Copyright © 2020-2023  润新知