spawning the appropriate platform-specific command line and reading its output (the text normally thrown up on the console window) :\temppython import os os popen('dir / 'readlines(['parts\ ''pp \ ''random bin\ ''spam txt\ ''temp bin\ ''temp txt\ 'lines read from shell command come back with trailing end-of-line characterbut it' easy enough to slice it offthe os popen result also gives us line iterator just like normal filesfor line in os popen('dir / ')print(line[:- ]parts pp random bin spam txt temp bin temp txt lines [line[:- for line in os popen('dir / ')lines ['parts''pp ''random bin''spam txt''temp bin''temp txt'for pipe objectsthe effect of iterators may be even more useful than simply avoiding loading the entire result into memory all at oncereadlines will always block the caller until the spawned program is completely finishedwhereas the iterator might not the dir and ls commands let us be specific about filename patterns to be matched and directory names to be listed by using name patternsagainwe're just running shell commands hereso anything you can type at shell prompt goesos popen('dir bin / 'readlines(['random bin\ ''temp bin\ 'os popen( ' :\cygwin\bin\ls bin'readlines(['random bin\ ''temp bin\ 'list(os popen( 'dir parts / ')['part \ ''part \ ''part \ ''part \ '[fname for fname in os popen( ' :\cygwin\bin\ls parts')['part \ ''part \ ''part \ ''part \ 'these calls use general tools and work as advertised as noted earlierthoughthe downsides of os popen are that it requires using platform-specific shell command and it incurs performance hit to start up an independent program in factdifferent listing tools may sometimes produce different resultslist(os popen( 'dir parts\part/ ')['part \ ''part \ ''part \ ''part \ 'directory tools