5

x,y, and z are folders in d:/

d:/x(current)

d:/y

d:/z

What is the best way to get paths of y and z folders from given x folder path.

2 Answers 2

5

Using pathlib module

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
Sign up to request clarification or add additional context in comments.

1 Comment

Short answer is, get the parent and iterate.
1

I think this should pretty much do what you want.

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.