Python Library: os (built-in)

ongoing note

15 Sep 2022

List everything in a folder

for x in os.listdir('/Volumes/Music'):
    print(x)

Listing everythin in subfolders too

for root, dirs, files in os.walk("/Volumes/Music"):
    print(f"{root=}")
    print(f"{dirs=}")
    print(f"{files=}")
    print()

prints:

root='/Volumes/Mus1c/The Roots'
dirs=['1995 Do You Want More?!!!??! (FLAC)', '1996 Illadelph Halflife (FLAC)', '1999 Things Fall Apart (FLAC)', '2002 Phrenology (FLAC)', '2004 The Tipping Point (FLAC)', '2006 Game Theory (FLAC)', '2010 How I Got Over (FLAC)', '2011 Undun (FLAC)']
files=[]

root='/Volumes/Mus1c/The Roots/1995 Do You Want More?!!!??! (FLAC)'
dirs=[]
files=["01 IntroThere's Something Goin' On (16 Bit).flac", '02 Proceed (16 Bit).flac', '03 Distortion To Static (16 Bit).flac', '04 Mellow My Man (16 Bit).flac', '05 I Remain Calm (16 Bit).flac', '06 Datskat (16 Bit).flac', '07 Lazy Afternoon (16 Bit).flac', '08  vs. Rahzel (16 Bit).flac', '09 Do You Want More!!!! (16 Bit).flac', '10 What Goes On Pt. 7 (16 Bit).flac', '11 Essaywhuman!!!! (16 Bit).flac', '12 Swept Away (16 Bit).flac', "13 You Ain't Fly (16 Bit).flac", '14 Silent Treatment (16 Bit).flac', '15 The Lesson Part 1 (16 Bit).flac', '16 The Unlocking (16 Bit).flac']

root='/Volumes/Music/The Streets/2011 Computers And Blues (M4A)'
dirs=[]
files=['01 Outside Inside.m4a', '02 Going Through Hell (feat. Robert Harvey of The Music).m4a', '03 Roof Of Your Car.m4a', '04 Puzzled By People.m4a', '05 Without Thinking.m4a', '06 Blip On A Screen.m4a', "07 Those That Don't Know.m4a", '08 Soldiers (feat. Robert Harvey of The Music).m4a', '09 We Can Never Be Friends.m4a', '10 ABC.m4a', '11 OMG.m4a', '12 Trying To Kill M.E..m4a', '13 Trust Me.m4a', '14 Lock The Locks (feat. Clare Maguire).m4a']

root='/Volumes/Music/The Ting Tings/2008 We Started Nothing (FLAC)'
dirs=[]
files=['01 Great DJ (16 Bit).flac', "02 That's Not My Name (16 Bit).flac", '03 Fruit Machine (16 Bit).flac', '04 Traffic Light (16 Bit).flac', '05 Shut Up And Let Me Go (16 Bit).flac', '06 Keep Your Head (16 Bit).flac', '07 Be The One (16 Bit).flac', '08 We Walk (16 Bit).flac', '09 Impacilla Carpisung (16 Bit).flac', '10 We Started Nothing (16 Bit).flac']

Notes:

  • folders without subfolders return empty dirs list.
  • folders only with subfolders return empty files list.

check if file

if os.path.isfile(full_path):

links

social