如何在python中获取兄弟文件夹路径?

let*_*eck 4 python path python-3.x pathlib

x, y, 和z是文件夹d:/

d:/x(当前的)

d:/y

d:/z

什么是得到的路径的最佳方式y,并z从给定的文件夹中x的文件夹路径。

bal*_*lki 5

使用 pathlib 模块

In [39]: from pathlib import Path

In [40]: def get_siblings(path):
    ...:     parent = path.parent
    ...:     for x in parent.iterdir():
    ...:         if x.is_dir() and x != path:
    ...:             yield x
    ...:             

In [41]: for f in get_siblings(Path.cwd()):
    ...:     print(f)
    ...:     
/mnt
/snap
/lib
/bin
/tmp
/sbin
/usr
/lib64
/opt
/lib32
/boot
/etc
/media
Run Code Online (Sandbox Code Playgroud)


ASH*_*ASH 1

我认为这应该可以满足你的要求。

import os
from os.path import join, getsize
for root, dirs, files in os.walk('C:/your_path_here/'):
    print(root, "consumes", end=" ")
    print(sum(getsize(join(root, name)) for name in files), end=" ")
    print("bytes in", len(files), "non-directory files")
Run Code Online (Sandbox Code Playgroud)