The listdir() method returns the list of entries for the given directory. The method os.path.isfile() returns True if the given entry is a file.
Home
Public
Questions
Tags
Users
Companies
Collectives
ExploreCollectives
Teams
StackOverflowforTeams
–Startcollaboratingandsharingorganizationalknowledge.
CreateafreeTeam
WhyTeams?
Teams
CreatefreeTeam
CollectivesonStackOverflow
Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.
Learnmore
Teams
Q&Aforwork
Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.
Learnmore
HowdoIlistallfilesofadirectory?
AskQuestion
Asked
11years,9monthsago
Modified
12daysago
Viewed
6.7mtimes
3467
1236
Thisquestion'sanswersareacommunityeffort.Editexistinganswerstoimprovethispost.Itisnotcurrentlyacceptingnewanswersorinteractions.
HowcanIlistallfilesofadirectoryinPythonandaddthemtoalist?
pythondirectory
Share
Follow
editedOct22,2017at2:35
Ioannis
9,2151010goldbadges7171silverbadges104104bronzebadges
askedJul8,2010at19:31
duhhunjonnduhhunjonn
43.8k1111goldbadges2626silverbadges1515bronzebadges
1
27
RelatedtoHowtogetalistofsubdirectories
– rds
Jan5,2012at9:32
Commentsdisabledondeleted/lockedposts/reviews
|
21Answers
21
Sortedby:
Resettodefault
Highestscore(default)
Datemodified(newestfirst)
Datecreated(oldestfirst)
Helpusimproveouranswers.Aretheanswersbelowsortedinawaythatputsthebestansweratornearthetop?
Takeashortsurvey
I’mnotinterested
5744
os.listdir()returnseverythinginsideadirectory--includingbothfilesanddirectories.
os.path'sisfile()canbeusedtoonlylistfiles:
fromosimportlistdir
fromos.pathimportisfile,join
onlyfiles=[fforfinlistdir(mypath)ifisfile(join(mypath,f))]
Alternatively,os.walk()yieldstwolistsforeachdirectoryitvisits--oneforfilesandonefordirs.Ifyouonlywantthetopdirectoryyoucanbreakthefirsttimeityields:
fromosimportwalk
f=[]
for(dirpath,dirnames,filenames)inwalk(mypath):
f.extend(filenames)
break
or,shorter:
fromosimportwalk
filenames=next(walk(mypath),(None,None,[]))[2]#[]ifnofile
Share
Improvethisanswer
Follow
editedApr19at1:55
MateenUlhaq
21.2k1616goldbadges7979silverbadges123123bronzebadges
answeredJul8,2010at21:01
pycruftpycruft
60.7k11goldbadge1616silverbadges1111bronzebadges
7
124
Abitsimpler:(_,_,filenames)=walk(mypath).next()(ifyouareconfidentthatthewalkwillreturnatleastonevalue,whichitshould.)
– misterbee
Jul14,2013at20:56
11
Slightmodificationtostorefullpaths:for(dirpath,dirnames,filenames)inos.walk(mypath):checksum_files.extend(os.path.join(dirpath,filename)forfilenameinfilenames)break
– okigan
Sep23,2013at21:31
183
f.extend(filenames)isnotactuallyequivalenttof=f+filenames.extendwillmodifyfin-place,whereasaddingcreatesanewlistinanewmemorylocation.Thismeansextendisgenerallymoreefficientthan+,butitcansometimesleadtoconfusionifmultipleobjectsholdreferencestothelist.Lastly,it'sworthnotingthatf+=filenamesisequivalenttof.extend(filenames),notf=f+filenames.
– BenjaminHodgson
Oct22,2013at8:55
39
@misterbee,yoursolutionisthebest,justonesmallimprovement:_,_,filenames=next(walk(mypath),(None,None,[]))
– bgusach
Mar5,2015at7:36
6
f+=filenamesisequivalenttoextendandnottheotherwayaround???Jeez.
– Umagon
Jul12,2020at9:18
|
Show2morecomments
2299
Ipreferusingtheglobmodule,asitdoespatternmatchingandexpansion.
importglob
print(glob.glob("/home/adam/*"))
Itdoespatternmatchingintuitively
importglob
#Allfilesanddirectoriesendingwith.txtandthatdon'tbeginwithadot:
print(glob.glob("/home/adam/*.txt"))
#Allfilesanddirectoriesendingwith.txtwithdepthof2folders,ignoringnamesbeginningwithadot:
print(glob.glob("/home/adam/*/*.txt"))
Itwillreturnalistwiththequeriedfilesanddirectories:
['/home/adam/file1.txt','/home/adam/file2.txt',....]
Notethatglobignoresfilesanddirectoriesthatbeginwithadot.,asthoseareconsideredhiddenfilesanddirectories,unlessthepatternissomethinglike.*.
Useglob.escapetoescapestringsthatarenotmeanttobepatterns:
print(glob.glob(glob.escape(directory_name)+"/*.txt"))
Share
Improvethisanswer
Follow
editedJan11at13:56
Flimm
114k3737goldbadges224224silverbadges238238bronzebadges
answeredJul9,2010at18:13
adamkadamk
42.3k77goldbadges4949silverbadges5656bronzebadges
5
20
that'sashortcutforlistdir+fnmatchdocs.python.org/library/fnmatch.html#fnmatch.fnmatch
– Stefano
Jul1,2011at13:03
52
toclarify,thisdoesnotreturnthe"fullpath";itsimplyreturnstheexpansionoftheglob,whateveritmaybe.E.g.,given/home/user/foo/bar/hello.txt,then,ifrunningindirectoryfoo,theglob("bar/*.txt")willreturnbar/hello.txt.Therearecaseswhenyoudoinfactwantthefull(i.e.,absolute)path;forthosecases,seestackoverflow.com/questions/51520/…
– michael
Aug16,2016at12:07
1
Related:findfilesrecursivelywithglob:stackoverflow.com/a/2186565/4561887
– GabrielStaples
Sep3,2018at3:25
10
doesn'tanswerthisquestion.glob.glob("*")would.
– Jean-FrançoisFabre
♦
May17,2019at18:36
2
Isthereawaytoensuretheitemsreturnedfromglobareonlyfiles?Iaskregardingtheinstancewherefilescouldexistwithoutextensions(orotherscenarioswherefilesandfoldersareindistinguishablepurelyfromtheirpathstrings).InotethisisanappropriateclarificationhereastheOPdidnotspecifywhetherornottheirfileshaveextensionsonthem.
– aamailhot
Aug11,2021at21:57
Addacomment
|
1420
listinthecurrentdirectory
Withlistdirinosmoduleyougetthefilesandthefoldersinthecurrentdir
importos
arr=os.listdir()
Lookinginadirectory
arr=os.listdir('c:\\files')
withglobyoucanspecifyatypeoffiletolistlikethis
importglob
txtfiles=[]
forfileinglob.glob("*.txt"):
txtfiles.append(file)
or
mylist=[fforfinglob.glob("*.txt")]
getthefullpathofonlyfilesinthecurrentdirectory
importos
fromosimportlistdir
fromos.pathimportisfile,join
cwd=os.getcwd()
onlyfiles=[os.path.join(cwd,f)forfinos.listdir(cwd)if
os.path.isfile(os.path.join(cwd,f))]
print(onlyfiles)
['G:\\getfilesname\\getfilesname.py','G:\\getfilesname\\example.txt']
Gettingthefullpathnamewithos.path.abspath
Yougetthefullpathinreturn
importos
files_path=[os.path.abspath(x)forxinos.listdir()]
print(files_path)
['F:\\documenti\applications.txt','F:\\documenti\collections.txt']
Walk:goingthroughsubdirectories
os.walkreturnstheroot,thedirectorieslistandthefileslist,thatiswhyIunpackedtheminr,d,fintheforloop;it,then,looksforotherfilesanddirectoriesinthesubfoldersoftherootandsoonuntiltherearenosubfolders.
importos
#Gettingthecurrentworkdirectory(cwd)
thisdir=os.getcwd()
#r=root,d=directories,f=files
forr,d,finos.walk(thisdir):
forfileinf:
iffile.endswith(".docx"):
print(os.path.join(r,file))
Togoupinthedirectorytree
#Method1
x=os.listdir('..')
#Method2
x=os.listdir('/')
Getfilesofaparticularsubdirectorywithos.listdir()
importos
x=os.listdir("./content")
os.walk('.')-currentdirectory
importos
arr=next(os.walk('.'))[2]
print(arr)
>>>['5bs_Turismo1.pdf','5bs_Turismo1.pptx','esperienza.txt']
next(os.walk('.'))andos.path.join('dir','file')
importos
arr=[]
ford,r,finnext(os.walk("F:\\_python")):
forfileinf:
arr.append(os.path.join(r,file))
forfinarr:
print(files)
>>>F:\\_python\\dict_class.py
>>>F:\\_python\\programmi.txt
next...walk
[os.path.join(r,file)forr,d,finnext(os.walk("F:\\_python"))forfileinf]
>>>['F:\\_python\\dict_class.py','F:\\_python\\programmi.txt']
os.walk
x=[os.path.join(r,file)forr,d,finos.walk("F:\\_python")forfileinf]
print(x)
>>>['F:\\_python\\dict.py','F:\\_python\\progr.txt','F:\\_python\\readl.py']
os.listdir()-getonlytxtfiles
arr_txt=[xforxinos.listdir()ifx.endswith(".txt")]
Usingglobtogetthefullpathofthefiles
frompathimportpath
fromglobimportglob
x=[path(f).abspath()forfinglob("F:\\*.txt")]
Usingos.path.isfiletoavoiddirectoriesinthelist
importos.path
listOfFiles=[fforfinos.listdir()ifos.path.isfile(f)]
UsingpathlibfromPython3.4
importpathlib
flist=[]
forpinpathlib.Path('.').iterdir():
ifp.is_file():
print(p)
flist.append(p)
Withlistcomprehension:
flist=[pforpinpathlib.Path('.').iterdir()ifp.is_file()]
Useglobmethodinpathlib.Path()
importpathlib
py=pathlib.Path().glob("*.py")
Getallandonlyfileswithos.walk:checksonlyinthethirdelementreturned,i.e.thelistofthefiles
importos
x=[i[2]foriinos.walk('.')]
y=[]
fortinx:
forfint:
y.append(f)
Getonlyfileswithnextinadirectory:returnsonlythefileintherootfolder
importos
x=next(os.walk('F://python'))[2]
Getonlydirectorieswithnextandwalkinadirectory,becauseinthe[1]elementtherearethefoldersonly
importos
next(os.walk('F://python'))[1]#forthecurrentdiruse('.')
>>>['python3','others']
Getallthesubdirnameswithwalk
forr,d,finos.walk("F:\\_python"):
fordirsind:
print(dirs)
os.scandir()fromPython3.5andgreater
importos
x=[f.nameforfinos.scandir()iff.is_file()]
#Anotherexamplewith`scandir`(alittlevariationfromdocs.python.org)
#Thisoneismoreefficientthan`os.listdir`.
#Inthiscase,itshowsthefilesonlyinthecurrentdirectory
#wherethescriptisexecuted.
importos
withos.scandir()asi:
forentryini:
ifentry.is_file():
print(entry.name)
Share
Improvethisanswer
Follow
editedApr12at17:41
xxx
9021010silverbadges2222bronzebadges
answeredJan3,2017at15:36
PythonProgrammiPythonProgrammi
20.2k33goldbadges3636silverbadges3434bronzebadges
10
88
Thisisamish-mashoftoomanyanswerstoquestionsnotaskedhere.Itmayalsobeworthexplainingwhatthecaveatsorrecommendedapproachesare.I'mnobetteroffknowingonewayversus20waystodothesamethingunlessIalsoknowwhichismoreappropriatetousewhen.
– cs95
Jan27,2020at9:27
3
Ok,ASAPIwilltakealookatmyanswerandtrytomakeitmorecleanandwithmoreusefulinformationsaboutthedifferenceamongthemethodsetc.
– PythonProgrammi
Jan29,2020at19:56
1
Youshouldnotdeterminefile'sextensionbycheckingifthefilenamecontainsasubstring.Thatmightcausemanytroubles.Irecommendtoalwayscheckifthefilenameendswiththeparticularsubstring.
– ni1ight
Mar2,2020at14:38
4
Suchcompilationscanbehelpful,butthisanswerinparticularaddsnovaluetotheexistinganswers.Justtogiveanexample,[fforfinglob.glob("*.txt")]isequivalenttoglob.glob("*.txt")andwarrantsnoextrasectioninthiswriteup.Itisalsoverywordyandwithlotsofspacing.Animprovementcouldbemadebyaddingexplanationsorpointingoutdifferencesinsteadoflistingyetanothervariant.
– TurunAmbartanen
Oct12,2020at10:20
1
Thanksforthecomment,youarerightofcourseandIwillfollowyouradvisesasaptomakeitmoreuseful,maybeintheseyearsIcouldmakesomebetteransewers.Justgivemesomedaystore-elaborateit.
– PythonProgrammi
Oct15,2020at11:07
|
Show5morecomments
947
importos
os.listdir("somedirectory")
willreturnalistofallfilesanddirectoriesin"somedirectory".
Share
Improvethisanswer
Follow
editedJul13,2016at19:05
csano
12.7k22goldbadges2626silverbadges4444bronzebadges
answeredJul8,2010at19:35
sepp2ksepp2k
353k5252goldbadges661661silverbadges667667bronzebadges
2
12
Thisreturnstherelativepathofthefiles,ascomparedwiththefullpathreturnedbyglob.glob
– xji
May17,2016at14:32
25
@JIXiang:os.listdir()alwaysreturnsmerefilenames(notrelativepaths).Whatglob.glob()returnsisdrivenbythepathformatoftheinputpattern.
– mklement0
Nov30,2016at18:14
Addacomment
|
183
Aone-linesolutiontogetonlylistoffiles(nosubdirectories):
filenames=next(os.walk(path))[2]
orabsolutepathnames:
paths=[os.path.join(path,fn)forfninnext(os.walk(path))[2]]
Share
Improvethisanswer
Follow
editedSep22,2019at20:40
ohe
3,26933goldbadges2424silverbadges4646bronzebadges
answeredJan18,2014at17:42
RemiRemi
19.3k88goldbadges5353silverbadges4141bronzebadges
3
7
Onlyaone-linerifyou'vealreadyimportos.Seemslessconcisethanglob()tome.
– ArtOfWarfare
Nov28,2014at20:22
4
problemwithglobisthatafoldercalled'something.something'wouldbereturnedbyglob('/home/adam/*.*')
– Remi
Dec1,2014at9:08
6
OnOSX,there'ssomethingcalledabundle.It'sadirectorywhichshouldgenerallybetreatedasafile(likea.tar).Wouldyouwantthosetreatedasafileoradirectory?Usingglob()wouldtreatitasafile.Yourmethodwouldtreatitasadirectory.
– ArtOfWarfare
Dec1,2014at19:44
Addacomment
|
151
GettingFullFilePathsFromaDirectoryandAllItsSubdirectories
importos
defget_filepaths(directory):
"""
Thisfunctionwillgeneratethefilenamesinadirectory
treebywalkingthetreeeithertop-downorbottom-up.Foreach
directoryinthetreerootedatdirectorytop(includingtopitself),
ityieldsa3-tuple(dirpath,dirnames,filenames).
"""
file_paths=[]#Listwhichwillstoreallofthefullfilepaths.
#Walkthetree.
forroot,directories,filesinos.walk(directory):
forfilenameinfiles:
#Jointhetwostringsinordertoformthefullfilepath.
filepath=os.path.join(root,filename)
file_paths.append(filepath)#Addittothelist.
returnfile_paths#Self-explanatory.
#Runtheabovefunctionandstoreitsresultsinavariable.
full_file_paths=get_filepaths("/Users/johnny/Desktop/TEST")
ThepathIprovidedintheabovefunctioncontained3files—twoofthemintherootdirectory,andanotherinasubfoldercalled"SUBFOLDER."Youcannowdothingslike:
printfull_file_pathswhichwillprintthelist:
['/Users/johnny/Desktop/TEST/file1.txt','/Users/johnny/Desktop/TEST/file2.txt','/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat']
Ifyou'dlike,youcanopenandreadthecontents,orfocusonlyonfileswiththeextension".dat"likeinthecodebelow:
forfinfull_file_paths:
iff.endswith(".dat"):
printf
/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat
Share
Improvethisanswer
Follow
editedSep26,2019at2:29
answeredOct11,2013at0:55
JohnnyJohnny
2,01811goldbadge1313silverbadges1010bronzebadges
0
Addacomment
|
98
Sinceversion3.4therearebuiltiniteratorsforthiswhicharealotmoreefficientthanos.listdir():
pathlib:Newinversion3.4.
>>>importpathlib
>>>[pforpinpathlib.Path('.').iterdir()ifp.is_file()]
AccordingtoPEP428,theaimofthepathliblibraryistoprovideasimplehierarchyofclassestohandlefilesystempathsandthecommonoperationsusersdooverthem.
os.scandir():Newinversion3.5.
>>>importos
>>>[entryforentryinos.scandir('.')ifentry.is_file()]
Notethatos.walk()usesos.scandir()insteadofos.listdir()fromversion3.5,anditsspeedgotincreasedby2-20timesaccordingtoPEP471.
LetmealsorecommendreadingShadowRanger'scommentbelow.
Share
Improvethisanswer
Follow
editedMay23,2018at18:41
PeterMortensen
29.9k2121goldbadges9999silverbadges124124bronzebadges
answeredJun18,2015at20:58
SzieberthAdamSzieberthAdam
3,74111goldbadge2121silverbadges3131bronzebadges
4
1
Thanks!Ithinkitistheonlysolutionnotreturningdirectlyalist.Couldusep.nameinsteadofthefirstpalternativelyifpreferred.
– jeromej
Jun22,2015at12:36
1
Welcome!Iwouldprefergeneratingpathlib.Path()instancessincetheyhavemanyusefulmethodsIwouldnotwanttowastewaste.Youcanalsocallstr(p)onthemforpathnames.
– SzieberthAdam
Jul13,2015at14:56
7
Note:Theos.scandirsolutionisgoingtobemoreefficientthanos.listdirwithanos.path.is_filecheckorthelike,evenifyouneedalist(soyoudon'tbenefitfromlazyiteration),becauseos.scandirusesOSprovidedAPIsthatgiveyoutheis_fileinformationforfreeasititerates,noper-fileroundtriptothedisktostatthematall(onWindows,theDirEntrysgetyoucompletestatinfoforfree,on*NIXsystemsitneedstostatforinfobeyondis_file,is_dir,etc.,butDirEntrycachesonfirststatforconvenience).
– ShadowRanger
Nov20,2015at22:38
1
Youcanalsouseentry.nametogetonlythefilename,orentry.pathtogetitsfullpath.Nomoreos.path.join()allovertheplace.
– user136036
Mar28,2017at20:26
Addacomment
|
66
Preliminarynotes
Althoughthere'sacleardifferentiationbetweenfileanddirectorytermsinthequestiontext,somemayarguethatdirectoriesareactuallyspecialfiles
Thestatement:"allfilesofadirectory"canbeinterpretedintwoways:
Alldirect(orlevel1)descendantsonly
Alldescendantsinthewholedirectorytree(includingtheonesinsub-directories)
Whenthequestionwasasked,IimaginethatPython2,wastheLTSversion,howeverthecodesampleswillberunbyPython3(.5)(I'llkeepthemasPython2compliantaspossible;also,anycodebelongingtoPythonthatI'mgoingtopost,isfromv3.5.4-unlessotherwisespecified).Thathasconsequencesrelatedtoanotherkeywordinthequestion:"addthemintoalist":
InprePython2.2versions,sequences(iterables)weremostlyrepresentedbylists(tuples,sets,...)
InPython2.2,theconceptofgenerator([Python.Wiki]:Generators)-courtesyof[Python3]:Theyieldstatement)-wasintroduced.Astimepassed,generatorcounterpartsstartedtoappearforfunctionsthatreturned/workedwithlists
InPython3,generatoristhedefaultbehavior
Notsureifreturningalistisstillmandatory(orageneratorwoulddoaswell),butpassingageneratortothelistconstructor,willcreatealistoutofit(andalsoconsumeit).Theexamplebelowillustratesthedifferenceson[Python3]:map(function,iterable,...)
>>>importsys
>>>sys.version
'2.7.10(default,Mar82016,15:02:46)[MSCv.160064bit(AMD64)]'
>>>m=map(lambdax:x,[1,2,3])#Justadummylambdafunction
>>>m,type(m)
([1,2,3],)
>>>len(m)
3
>>>importsys
>>>sys.version
'3.5.4(v3.5.4:3f56838,Aug82017,02:17:05)[MSCv.190064bit(AMD64)]'
>>>m=map(lambdax:x,[1,2,3])
>>>m,type(m)
(,)
>>>len(m)
Traceback(mostrecentcalllast):
File"",line1,in
TypeError:objectoftype'map'hasnolen()
>>>lm0=list(m)#Buildalistfromthegenerator
>>>lm0,type(lm0)
([1,2,3],)
>>>
>>>lm1=list(m)#Buildalistfromthesamegenerator
>>>lm1,type(lm1)#Emptylistnow-generatoralreadyconsumed
([],)
Theexampleswillbebasedonadirectorycalledroot_dirwiththefollowingstructure(thisexampleisforWin,butI'musingthesametreeonLnxaswell):
E:\Work\Dev\StackOverflow\q003207219>tree/f"root_dir"
FolderPATHlistingforvolumeWork
Volumeserialnumberis000000293655:6FED
E:\WORK\DEV\STACKOVERFLOW\Q003207219\ROOT_DIR
¦file0
¦file1
¦
+---dir0
¦+---dir00
¦¦¦file000
¦¦¦
¦¦+---dir000
¦¦file0000
¦¦
¦+---dir01
¦¦file010
¦¦file011
¦¦
¦+---dir02
¦+---dir020
¦+---dir0200
+---dir1
¦file10
¦file11
¦file12
¦
+---dir2
¦¦file20
¦¦
¦+---dir20
¦file200
¦
+---dir3
Solutions
Programmaticapproaches:
[Python3]:os.listdir(path='.')
Returnalistcontainingthenamesoftheentriesinthedirectorygivenbypath.Thelistisinarbitraryorder,anddoesnotincludethespecialentries'.'and'..'...
>>>importos
>>>root_dir="root_dir"#Pathrelativetocurrentdir(os.getcwd())
>>>
>>>os.listdir(root_dir)#Listalltheitemsinroot_dir
['dir0','dir1','dir2','dir3','file0','file1']
>>>
>>>[itemforiteminos.listdir(root_dir)ifos.path.isfile(os.path.join(root_dir,item))]#Filteritemsandonlykeepfiles(stripoutdirectories)
['file0','file1']
Amoreelaborateexample(code_os_listdir.py):
importos
frompprintimportpformat
def_get_dir_content(path,include_folders,recursive):
entries=os.listdir(path)
forentryinentries:
entry_with_path=os.path.join(path,entry)
ifos.path.isdir(entry_with_path):
ifinclude_folders:
yieldentry_with_path
ifrecursive:
forsub_entryin_get_dir_content(entry_with_path,include_folders,recursive):
yieldsub_entry
else:
yieldentry_with_path
defget_dir_content(path,include_folders=True,recursive=True,prepend_folder_name=True):
path_len=len(path)+len(os.path.sep)
foritemin_get_dir_content(path,include_folders,recursive):
yielditemifprepend_folder_nameelseitem[path_len:]
def_get_dir_content_old(path,include_folders,recursive):
entries=os.listdir(path)
ret=list()
forentryinentries:
entry_with_path=os.path.join(path,entry)
ifos.path.isdir(entry_with_path):
ifinclude_folders:
ret.append(entry_with_path)
ifrecursive:
ret.extend(_get_dir_content_old(entry_with_path,include_folders,recursive))
else:
ret.append(entry_with_path)
returnret
defget_dir_content_old(path,include_folders=True,recursive=True,prepend_folder_name=True):
path_len=len(path)+len(os.path.sep)
return[itemifprepend_folder_nameelseitem[path_len:]foritemin_get_dir_content_old(path,include_folders,recursive)]
defmain():
root_dir="root_dir"
ret0=get_dir_content(root_dir,include_folders=True,recursive=True,prepend_folder_name=True)
lret0=list(ret0)
print(ret0,len(lret0),pformat(lret0))
ret1=get_dir_content_old(root_dir,include_folders=False,recursive=True,prepend_folder_name=False)
print(len(ret1),pformat(ret1))
if__name__=="__main__":
main()
Notes:
Therearetwoimplementations:
Onethatusesgenerators(ofcoursehereitseemsuseless,sinceIimmediatelyconverttheresulttoalist)
Theclassicone(functionnamesendingin_old)
Recursionisused(togetintosubdirectories)
Foreachimplementationtherearetwofunctions:
Onethatstartswithanunderscore(_):"private"(shouldnotbecalleddirectly)-thatdoesallthework
Thepublicone(wrapperoverprevious):itjuststripsofftheinitialpath(ifrequired)fromthereturnedentries.It'sanuglyimplementation,butit'stheonlyideathatIcouldcomewithatthispoint
Intermsofperformance,generatorsaregenerallyalittlebitfaster(consideringbothcreationanditerationtimes),butIdidn'ttesttheminrecursivefunctions,andalsoIamiteratinginsidethefunctionoverinnergenerators-don'tknowhowperformancefriendlyisthat
Playwiththeargumentstogetdifferentresults
Output:
(py35x64_test)E:\Work\Dev\StackOverflow\q003207219>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe""code_os_listdir.py"
22['root_dir\\dir0',
'root_dir\\dir0\\dir00',
'root_dir\\dir0\\dir00\\dir000',
'root_dir\\dir0\\dir00\\dir000\\file0000',
'root_dir\\dir0\\dir00\\file000',
'root_dir\\dir0\\dir01',
'root_dir\\dir0\\dir01\\file010',
'root_dir\\dir0\\dir01\\file011',
'root_dir\\dir0\\dir02',
'root_dir\\dir0\\dir02\\dir020',
'root_dir\\dir0\\dir02\\dir020\\dir0200',
'root_dir\\dir1',
'root_dir\\dir1\\file10',
'root_dir\\dir1\\file11',
'root_dir\\dir1\\file12',
'root_dir\\dir2',
'root_dir\\dir2\\dir20',
'root_dir\\dir2\\dir20\\file200',
'root_dir\\dir2\\file20',
'root_dir\\dir3',
'root_dir\\file0',
'root_dir\\file1']
11['dir0\\dir00\\dir000\\file0000',
'dir0\\dir00\\file000',
'dir0\\dir01\\file010',
'dir0\\dir01\\file011',
'dir1\\file10',
'dir1\\file11',
'dir1\\file12',
'dir2\\dir20\\file200',
'dir2\\file20',
'file0',
'file1']
[Python3]:os.scandir(path='.')(Python3.5+,backport:[PyPI]:scandir)
Returnaniteratorofos.DirEntryobjectscorrespondingtotheentriesinthedirectorygivenbypath.Theentriesareyieldedinarbitraryorder,andthespecialentries'.'and'..'arenotincluded.
Usingscandir()insteadoflistdir()cansignificantlyincreasetheperformanceofcodethatalsoneedsfiletypeorfileattributeinformation,becauseos.DirEntryobjectsexposethisinformationiftheoperatingsystemprovidesitwhenscanningadirectory.Allos.DirEntrymethodsmayperformasystemcall,butis_dir()andis_file()usuallyonlyrequireasystemcallforsymboliclinks;os.DirEntry.stat()alwaysrequiresasystemcallonUnixbutonlyrequiresoneforsymboliclinksonWindows.
>>>importos
>>>root_dir=os.path.join(".","root_dir")#Explicitlyprependingcurrentdirectory
>>>root_dir
'.\\root_dir'
>>>
>>>scandir_iterator=os.scandir(root_dir)
>>>scandir_iterator
>>>[item.pathforiteminscandir_iterator]
['.\\root_dir\\dir0','.\\root_dir\\dir1','.\\root_dir\\dir2','.\\root_dir\\dir3','.\\root_dir\\file0','.\\root_dir\\file1']
>>>
>>>[item.pathforiteminscandir_iterator]#Willyieldanemptylistasitwasconsumedbypreviousiteration(automaticallyperformedbythelistcomprehension)
[]
>>>
>>>scandir_iterator=os.scandir(root_dir)#Reinitializethegenerator
>>>foriteminscandir_iterator:
...ifos.path.isfile(item.path):
...print(item.name)
...
file0
file1
Notes:
It'ssimilartoos.listdir
Butit'salsomoreflexible(andoffersmorefunctionality),morePythonic(andinsomecases,faster)
[Python3]:os.walk(top,topdown=True,onerror=None,followlinks=False)
Generatethefilenamesinadirectorytreebywalkingthetreeeithertop-downorbottom-up.Foreachdirectoryinthetreerootedatdirectorytop(includingtopitself),ityieldsa3-tuple(dirpath,dirnames,filenames).
>>>importos
>>>root_dir=os.path.join(os.getcwd(),"root_dir")#Specifythefullpath
>>>root_dir
'E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir'
>>>
>>>walk_generator=os.walk(root_dir)
>>>root_dir_entry=next(walk_generator)#Firstentrycorrespondstotherootdir(passedasanargument)
>>>root_dir_entry
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir',['dir0','dir1','dir2','dir3'],['file0','file1'])
>>>
>>>root_dir_entry[1]+root_dir_entry[2]#Displaydirsandfiles(directdescendants)inasinglelist
['dir0','dir1','dir2','dir3','file0','file1']
>>>
>>>[os.path.join(root_dir_entry[0],item)foriteminroot_dir_entry[1]+root_dir_entry[2]]#Displayalltheentriesinthepreviouslistbytheirfullpath
['E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0','E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir1','E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir2','E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir3','E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\file0','E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\file1']
>>>
>>>forentryinwalk_generator:#Displaytherestoftheelements(correspondingtoeverysubdir)
...print(entry)
...
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0',['dir00','dir01','dir02'],[])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir00',['dir000'],['file000'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir00\\dir000',[],['file0000'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir01',[],['file010','file011'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir02',['dir020'],[])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir02\\dir020',['dir0200'],[])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir0\\dir02\\dir020\\dir0200',[],[])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir1',[],['file10','file11','file12'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir2',['dir20'],['file20'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir2\\dir20',[],['file200'])
('E:\\Work\\Dev\\StackOverflow\\q003207219\\root_dir\\dir3',[],[])
Notes:
Underthescenes,itusesos.scandir(os.listdironolderversions)
Itdoestheheavyliftingbyrecurringinsubfolders
[Python3]:glob.glob(pathname,*,recursive=False)([Python3]:glob.iglob(pathname,*,recursive=False))
Returnapossibly-emptylistofpathnamesthatmatchpathname,whichmustbeastringcontainingapathspecification.pathnamecanbeeitherabsolute(like/usr/src/Python-1.5/Makefile)orrelative(like../../Tools/*/*.gif),andcancontainshell-stylewildcards.Brokensymlinksareincludedintheresults(asintheshell)....Changedinversion3.5:Supportforrecursiveglobsusing“**”.
>>>importglob,os
>>>wildcard_pattern="*"
>>>root_dir=os.path.join("root_dir",wildcard_pattern)#Matcheveryfile/dirname
>>>root_dir
'root_dir\\*'
>>>
>>>glob_list=glob.glob(root_dir)
>>>glob_list
['root_dir\\dir0','root_dir\\dir1','root_dir\\dir2','root_dir\\dir3','root_dir\\file0','root_dir\\file1']
>>>
>>>[item.replace("root_dir"+os.path.sep,"")foriteminglob_list]#Stripthedirnameandthepathseparatorfrombegining
['dir0','dir1','dir2','dir3','file0','file1']
>>>
>>>forentryinglob.iglob(root_dir+"*",recursive=True):
...print(entry)
...
root_dir\
root_dir\dir0
root_dir\dir0\dir00
root_dir\dir0\dir00\dir000
root_dir\dir0\dir00\dir000\file0000
root_dir\dir0\dir00\file000
root_dir\dir0\dir01
root_dir\dir0\dir01\file010
root_dir\dir0\dir01\file011
root_dir\dir0\dir02
root_dir\dir0\dir02\dir020
root_dir\dir0\dir02\dir020\dir0200
root_dir\dir1
root_dir\dir1\file10
root_dir\dir1\file11
root_dir\dir1\file12
root_dir\dir2
root_dir\dir2\dir20
root_dir\dir2\dir20\file200
root_dir\dir2\file20
root_dir\dir3
root_dir\file0
root_dir\file1
Notes:
Usesos.listdir
Forlargetrees(especiallyifrecursiveison),iglobispreferred
Allowsadvancedfilteringbasedonname(duetothewildcard)
[Python3]:classpathlib.Path(*pathsegments)(Python3.4+,backport:[PyPI]:pathlib2)
>>>importpathlib
>>>root_dir="root_dir"
>>>root_dir_instance=pathlib.Path(root_dir)
>>>root_dir_instance
WindowsPath('root_dir')
>>>root_dir_instance.name
'root_dir'
>>>root_dir_instance.is_dir()
True
>>>
>>>[item.nameforiteminroot_dir_instance.glob("*")]#Wildcardsearchingforalldirectdescendants
['dir0','dir1','dir2','dir3','file0','file1']
>>>
>>>[os.path.join(item.parent.name,item.name)foriteminroot_dir_instance.glob("*")ifnotitem.is_dir()]#Displaypaths(includingparent)forfilesonly
['root_dir\\file0','root_dir\\file1']
Notes:
Thisisonewayofachievingourgoal
It'stheOOPstyleofhandlingpaths
Offerslotsoffunctionalities
[Python2]:dircache.listdir(path)(Python2only)
But,accordingto[GitHub]:python/cpython-(2.7)cpython/Lib/dircache.py,it'sjusta(thin)wrapperoveros.listdirwithcaching
deflistdir(path):
"""Listdirectorycontents,usingcache."""
try:
cached_mtime,list=cache[path]
delcache[path]
exceptKeyError:
cached_mtime,list=-1,[]
mtime=os.stat(path).st_mtime
ifmtime!=cached_mtime:
list=os.listdir(path)
list.sort()
cache[path]=mtime,list
returnlist
[man7]:OPENDIR(3)/[man7]:READDIR(3)/[man7]:CLOSEDIR(3)via[Python3]:ctypes-AforeignfunctionlibraryforPython(POSIXspecific)
ctypesisaforeignfunctionlibraryforPython.ItprovidesCcompatibledatatypes,andallowscallingfunctionsinDLLsorsharedlibraries.ItcanbeusedtowraptheselibrariesinpurePython.
code_ctypes.py:
#!/usr/bin/envpython3
importsys
fromctypesimportStructure,\
c_ulonglong,c_longlong,c_ushort,c_ubyte,c_char,c_int,\
CDLL,POINTER,\
create_string_buffer,get_errno,set_errno,cast
DT_DIR=4
DT_REG=8
char256=c_char*256
classLinuxDirent64(Structure):
_fields_=[
("d_ino",c_ulonglong),
("d_off",c_longlong),
("d_reclen",c_ushort),
("d_type",c_ubyte),
("d_name",char256),
]
LinuxDirent64Ptr=POINTER(LinuxDirent64)
libc_dll=this_process=CDLL(None,use_errno=True)
#ALWAYSsetargtypesandrestypeforfunctions,otherwiseit'sUB!!!
opendir=libc_dll.opendir
readdir=libc_dll.readdir
closedir=libc_dll.closedir
defget_dir_content(path):
ret=[path,list(),list()]
dir_stream=opendir(create_string_buffer(path.encode()))
if(dir_stream==0):
print("opendirreturnedNULL(errno:{:d})".format(get_errno()))
returnret
set_errno(0)
dirent_addr=readdir(dir_stream)
whiledirent_addr:
dirent_ptr=cast(dirent_addr,LinuxDirent64Ptr)
dirent=dirent_ptr.contents
name=dirent.d_name.decode()
ifdirent.d_type&DT_DIR:
ifnamenotin(".",".."):
ret[1].append(name)
elifdirent.d_type&DT_REG:
ret[2].append(name)
dirent_addr=readdir(dir_stream)
ifget_errno():
print("readdirreturnedNULL(errno:{:d})".format(get_errno()))
closedir(dir_stream)
returnret
defmain():
print("{:s}on{:s}\n".format(sys.version,sys.platform))
root_dir="root_dir"
entries=get_dir_content(root_dir)
print(entries)
if__name__=="__main__":
main()
Notes:
Itloadsthethreefunctionsfromlibc(loadedinthecurrentprocess)andcallsthem(formoredetailscheck[SO]:HowdoIcheckwhetherafileexistswithoutexceptions?(@CristiFati'sanswer)-lastnotesfromitem#4.).ThatwouldplacethisapproachveryclosetothePython/Cedge
LinuxDirent64isthectypesrepresentationofstructdirent64from[man7]:dirent.h(0P)(soaretheDT_constants)frommymachine:Ubtu16x64(4.10.0-40-genericandlibc6-dev:amd64).Onotherflavors/versions,thestructdefinitionmightdiffer,andifso,thectypesaliasshouldbeupdated,otherwiseitwillyieldUndefinedBehavior
Itreturnsdataintheos.walk'sformat.Ididn'tbothertomakeitrecursive,butstartingfromtheexistingcode,thatwouldbeafairlytrivialtask
EverythingisdoableonWinaswell,thedata(libraries,functions,structs,constants,...)differ
Output:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q003207219]>./code_ctypes.py
3.5.2(default,Nov122018,13:43:14)
[GCC5.4.020160609]onlinux
['root_dir',['dir2','dir1','dir3','dir0'],['file1','file0']]
[ActiveState.Docs]:win32file.FindFilesW(Winspecific)
Retrievesalistofmatchingfilenames,usingtheWindowsUnicodeAPI.AninterfacetotheAPIFindFirstFileW/FindNextFileW/Findclosefunctions.
>>>importos,win32file,win32con
>>>root_dir="root_dir"
>>>wildcard="*"
>>>root_dir_wildcard=os.path.join(root_dir,wildcard)
>>>entry_list=win32file.FindFilesW(root_dir_wildcard)
>>>len(entry_list)#Don'tdisplaythewholecontentasit'stoolong
8
>>>[entry[-2]forentryinentry_list]#Onlydisplaytheentrynames
['.','..','dir0','dir1','dir2','dir3','file0','file1']
>>>
>>>[entry[-2]forentryinentry_listifentry[0]&win32con.FILE_ATTRIBUTE_DIRECTORYandentry[-2]notin(".","..")]#Filterentriesandonlydisplaydirnames(exceptselfandparent)
['dir0','dir1','dir2','dir3']
>>>
>>>[os.path.join(root_dir,entry[-2])forentryinentry_listifentry[0]&(win32con.FILE_ATTRIBUTE_NORMAL|win32con.FILE_ATTRIBUTE_ARCHIVE)]#Onlydisplayfile"full"names
['root_dir\\file0','root_dir\\file1']
Notes:
win32file.FindFilesWispartof[GitHub]:mhammond/pywin32-PythonforWindows(pywin32)Extensions,whichisaPythonwrapperoverWINAPIs
ThedocumentationlinkisfromActiveState,asIdidn'tfindanyPyWin32officialdocumentation
Installsome(other)third-partypackagethatdoesthetrick
Mostlikely,willrelyonone(ormore)oftheabove(maybewithslightcustomizations)
Notes:
Codeismeanttobeportable(exceptplacesthattargetaspecificarea-whicharemarked)orcross:
platform(Nix,Win,)
Pythonversion(2,3,)
Multiplepathstyles(absolute,relatives)wereusedacrosstheabovevariants,toillustratethefactthatthe"tools"usedareflexibleinthisdirection
os.listdirandos.scandiruseopendir/readdir/closedir([MS.Docs]:FindFirstFileWfunction/[MS.Docs]:FindNextFileWfunction/[MS.Docs]:FindClosefunction)(via[GitHub]:python/cpython-(master)cpython/Modules/posixmodule.c)
win32file.FindFilesWusesthose(Winspecific)functionsaswell(via[GitHub]:mhammond/pywin32-(master)pywin32/win32/src/win32file.i)
_get_dir_content(frompoint#1.)canbeimplementedusinganyoftheseapproaches(somewillrequiremoreworkandsomeless)
Someadvancedfiltering(insteadofjustfilevs.dir)couldbedone:e.g.theinclude_foldersargumentcouldbereplacedbyanotherone(e.g.filter_func)whichwouldbeafunctionthattakesapathasanargument:filter_func=lambdax:True(thisdoesn'tstripoutanything)andinside_get_dir_contentsomethinglike:ifnotfilter_func(entry_with_path):continue(ifthefunctionfailsforoneentry,itwillbeskipped),butthemorecomplexthecodebecomes,thelongeritwilltaketoexecute
Notabene!Sincerecursionisused,ImustmentionthatIdidsometestsonmylaptop(Win10x64),totallyunrelatedtothisproblem,andwhentherecursionlevelwasreachingvaluessomewhereinthe(990..1000)range(recursionlimit-1000(default)),IgotStackOverflow:).Ifthedirectorytreeexceedsthatlimit(IamnotanFSexpert,soIdon'tknowifthatisevenpossible),thatcouldbeaproblem.
ImustalsomentionthatIdidn'ttrytoincreaserecursionlimitbecauseIhavenoexperienceinthearea(howmuchcanIincreaseitbeforehavingtoalsoincreasethestackatOSlevel),butintheorytherewillalwaysbethepossibilityforfailure,ifthedirdepthislargerthanthehighestpossiblerecursionlimit(onthatmachine)
Thecodesamplesarefordemonstrativepurposesonly.ThatmeansthatIdidn'ttakeintoaccounterrorhandling(Idon'tthinkthere'sanytry/except/else/finallyblock),sothecodeisnotrobust(thereasonis:tokeepitassimpleandshortaspossible).Forproduction,errorhandlingshouldbeaddedaswell
Otherapproaches:
UsePythononlyasawrapper
Everythingisdoneusinganothertechnology
ThattechnologyisinvokedfromPython
ThemostfamousflavorthatIknowiswhatIcallthesystemadministratorapproach:
UsePython(oranyprogramminglanguageforthatmatter)inordertoexecuteshellcommands(andparsetheiroutputs)
Someconsiderthisaneathack
Iconsideritmorelikealameworkaround(gainarie),astheactionperseisperformedfromshell(cmdinthiscase),andthusdoesn'thaveanythingtodowithPython.
Filtering(grep/findstr)oroutputformattingcouldbedoneonbothsides,butI'mnotgoingtoinsistonit.Also,Ideliberatelyusedos.systeminsteadofsubprocess.Popen.
(py35x64_test)E:\Work\Dev\StackOverflow\q003207219>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe"-c"importos;os.system(\"dir/broot_dir\")"
dir0
dir1
dir2
dir3
file0
file1
Ingeneralthisapproachistobeavoided,sinceifsomecommandoutputformatslightlydiffersbetweenOSversions/flavors,theparsingcodeshouldbeadaptedaswell;nottomentiondifferencesbetweenlocales).
Share
Improvethisanswer
Follow
editedMar21,2019at14:14
answeredJan23,2018at3:09
CristiFatiCristiFati
32.5k99goldbadges4545silverbadges7070bronzebadges
0
Addacomment
|
50
Ireallylikedadamk'sanswer,suggestingthatyouuseglob(),fromthemoduleofthesamename.Thisallowsyoutohavepatternmatchingwith*s.
Butasotherpeoplepointedoutinthecomments,glob()cangettrippedupoverinconsistentslashdirections.Tohelpwiththat,Isuggestyouusethejoin()andexpanduser()functionsintheos.pathmodule,andperhapsthegetcwd()functionintheosmodule,aswell.
Asexamples:
fromglobimportglob
#ReturneverythingunderC:\Users\adminthatcontainsafoldercalledwlp.
glob('C:\Users\admin\*\wlp')
Theaboveisterrible-thepathhasbeenhardcodedandwillonlyeverworkonWindowsbetweenthedrivenameandthe\sbeinghardcodedintothepath.
fromglobimportglob
fromos.pathimportjoin
#ReturneverythingunderUsers,admin,thatcontainsafoldercalledwlp.
glob(join('Users','admin','*','wlp'))
Theaboveworksbetter,butitreliesonthefoldernameUserswhichisoftenfoundonWindowsandnotsooftenfoundonotherOSs.Italsoreliesontheuserhavingaspecificname,admin.
fromglobimportglob
fromos.pathimportexpanduser,join
#Returneverythingundertheuserdirectorythatcontainsafoldercalledwlp.
glob(join(expanduser('~'),'*','wlp'))
Thisworksperfectlyacrossallplatforms.
Anothergreatexamplethatworksperfectlyacrossplatformsanddoessomethingabitdifferent:
fromglobimportglob
fromosimportgetcwd
fromos.pathimportjoin
#Returneverythingunderthecurrentdirectorythatcontainsafoldercalledwlp.
glob(join(getcwd(),'*','wlp'))
HopetheseexampleshelpyouseethepowerofafewofthefunctionsyoucanfindinthestandardPythonlibrarymodules.
Share
Improvethisanswer
Follow
editedMay23,2017at11:47
CommunityBot
111silverbadge
answeredJul9,2014at11:43
ArtOfWarfareArtOfWarfare
19.1k1616goldbadges126126silverbadges184184bronzebadges
1
4
Extraglobfun:startinginPython3.5,**worksaslongasyousetrecursive=True.Seethedocshere:docs.python.org/3.5/library/glob.html#glob.glob
– ArtOfWarfare
Jan26,2015at3:24
Addacomment
|
40
deflist_files(path):
#returnsalistofnames(withextension,withoutfullpath)ofallfiles
#infolderpath
files=[]
fornameinos.listdir(path):
ifos.path.isfile(os.path.join(path,name)):
files.append(name)
returnfiles
Share
Improvethisanswer
Follow
editedOct7,2014at18:30
answeredJun10,2014at16:16
ApogentusApogentus
5,98144goldbadges3131silverbadges3232bronzebadges
0
Addacomment
|
26
IfyouarelookingforaPythonimplementationoffind,thisisarecipeIuseratherfrequently:
fromfindtools.find_filesimport(find_files,Match)
#Recursivelyfindall*.shfilesin**/usr/bin**
sh_files_pattern=Match(filetype='f',name='*.sh')
found_files=find_files(path='/usr/bin',match=sh_files_pattern)
forfound_fileinfound_files:
printfound_file
SoImadeaPyPIpackageoutofitandthereisalsoaGitHubrepository.Ihopethatsomeonefindsitpotentiallyusefulforthiscode.
Share
Improvethisanswer
Follow
editedMay28,2017at23:17
PeterMortensen
29.9k2121goldbadges9999silverbadges124124bronzebadges
answeredApr10,2014at14:09
YauhenYakimovichYauhenYakimovich
12.9k77goldbadges5656silverbadges6565bronzebadges
0
Addacomment
|
16
Forgreaterresults,youcanuselistdir()methodoftheosmodulealongwithagenerator(ageneratorisapowerfuliteratorthatkeepsitsstate,remember?).Thefollowingcodeworksfinewithbothversions:Python2andPython3.
Here'sacode:
importos
deffiles(path):
forfileinos.listdir(path):
ifos.path.isfile(os.path.join(path,file)):
yieldfile
forfileinfiles("."):
print(file)
Thelistdir()methodreturnsthelistofentriesforthegivendirectory.Themethodos.path.isfile()returnsTrueifthegivenentryisafile.Andtheyieldoperatorquitsthefuncbutkeepsitscurrentstate,anditreturnsonlythenameoftheentrydetectedasafile.Alltheaboveallowsustoloopoverthegeneratorfunction.
Share
Improvethisanswer
Follow
editedMay17,2019at9:23
Georgy
9,83277goldbadges5757silverbadges6666bronzebadges
answeredJan9,2019at10:11
AndyJazzAndyJazz
35.4k1212goldbadges106106silverbadges172172bronzebadges
Addacomment
|
12
Returningalistofabsolutefilepaths,doesnotrecurseintosubdirectories
L=[os.path.join(os.getcwd(),f)forfinos.listdir('.')ifos.path.isfile(os.path.join(os.getcwd(),f))]
Share
Improvethisanswer
Follow
editedDec28,2014at3:27
CristianCiupitu
19.2k77goldbadges4848silverbadges7373bronzebadges
answeredJun13,2014at16:26
The2ndSonThe2ndSon
30722silverbadges77bronzebadges
2
2
Note:os.path.abspath(f)wouldbeasomewhatcheapersubstituteforos.path.join(os.getcwd(),f).
– ShadowRanger
May6,2017at0:14
1
I'dbemoreefficientstillifyoustartedwithcwd=os.path.abspath('.'),thenusedcwdinsteadof'.'andos.getcwd()throughouttoavoidloadsofredundantsystemcalls.
– MartijnPieters
♦
Dec5,2018at10:46
Addacomment
|
12
Awiseteachertoldmeoncethat:
Whenthereareseveralestablishedwaystodosomething,noneofthemisgoodforallcases.
Iwillthusaddasolutionforasubsetoftheproblem:quiteoften,weonlywanttocheckwhetherafilematchesastartstringandanendstring,withoutgoingintosubdirectories.Wewouldthuslikeafunctionthatreturnsalistoffilenames,like:
filenames=dir_filter('foo/baz',radical='radical',extension='.txt')
Ifyoucaretofirstdeclaretwofunctions,thiscanbedone:
deffile_filter(filename,radical='',extension=''):
"Checkifafilenamematchesaradicalandextension"
ifnotfilename:
returnFalse
filename=filename.strip()
return(filename.startswith(radical)andfilename.endswith(extension))
defdir_filter(dirname='',radical='',extension=''):
"Filterfilenamesindirectoryaccordingtoradicalandextension"
ifnotdirname:
dirname='.'
return[filenameforfilenameinos.listdir(dirname)
iffile_filter(filename,radical,extension)]
Thissolutioncouldbeeasilygeneralizedwithregularexpressions(andyoumightwanttoaddapatternargument,ifyoudonotwantyourpatternstoalwayssticktothestartorendofthefilename).
Share
Improvethisanswer
Follow
editedMar24,2019at7:17
answeredMar24,2019at7:07
fralaufralau
2,65111goldbadge2424silverbadges3737bronzebadges
0
Addacomment
|
11
importos
importos.path
defget_files(target_dir):
item_list=os.listdir(target_dir)
file_list=list()
foriteminitem_list:
item_dir=os.path.join(target_dir,item)
ifos.path.isdir(item_dir):
file_list+=get_files(item_dir)
else:
file_list.append(item_dir)
returnfile_list
HereIusearecursivestructure.
Share
Improvethisanswer
Follow
editedJul18,2018at13:44
AndrewRohne
15033silverbadges1010bronzebadges
answeredJun19,2018at12:03
pah8Jpah8J
75777silverbadges1515bronzebadges
1
Thesamecanbeachievedjustinonelinewithpathlib:filter(Path.is_file,Path().rglob('*'))
– Georgy
May17,2019at9:37
Addacomment
|
9
Usinggenerators
importos
defget_files(search_path):
for(dirpath,_,filenames)inos.walk(search_path):
forfilenameinfilenames:
yieldos.path.join(dirpath,filename)
list_files=get_files('.')
forfilenameinlist_files:
print(filename)
Share
Improvethisanswer
Follow
editedMay17,2017at15:35
answeredDec2,2016at7:01
shantanooshantanoo
3,59711goldbadge2323silverbadges3636bronzebadges
1
THANKS!exactlywhatIneeded.AlotoftheanswersatthetopareoutdatedandnotworkingwithPython3.9:)
– Wlad
Oct14,2021at8:01
Addacomment
|
8
AnotherveryreadablevariantforPython3.4+isusingpathlib.Path.glob:
frompathlibimportPath
folder='/foo'
[fforfinPath(folder).glob('*')iff.is_file()]
Itissimpletomakemorespecific,e.g.onlylookforPythonsourcefileswhicharenotsymboliclinks,alsoinallsubdirectories:
[fforfinPath(folder).glob('**/*.py')ifnotf.is_symlink()]
Share
Improvethisanswer
Follow
editedMay23,2018at19:25
PeterMortensen
29.9k2121goldbadges9999silverbadges124124bronzebadges
answeredMar28,2018at12:20
fhchlfhchl
63977silverbadges1717bronzebadges
Addacomment
|
5
ForPython2:
pipinstallrglob
Thendo
importrglob
file_list=rglob.rglob("/home/base/dir/","*")
printfile_list
Share
Improvethisanswer
Follow
editedFeb8,2021at16:00
PeterMortensen
29.9k2121goldbadges9999silverbadges124124bronzebadges
answeredOct19,2018at2:34
chris-piekarskichris-piekarski
97877silverbadges66bronzebadges
1
3
Whenanexternaldepcanbeavoided,doit.Whatistheaddedvalueofusinganexternaldependencywhenallyouneedisalreadyinthelanguage?
– Eric
Mar30,2021at13:45
Addacomment
|
4
Here'smygeneral-purposefunctionforthis.ItreturnsalistoffilepathsratherthanfilenamessinceIfoundthattobemoreuseful.Ithasafewoptionalargumentsthatmakeitversatile.Forinstance,Ioftenuseitwithargumentslikepattern='*.txt'orsubfolders=True.
importos
importfnmatch
deflist_paths(folder='.',pattern='*',case_sensitive=False,subfolders=False):
"""Returnalistofthefilepathsmatchingthepatterninthespecified
folder,optionallyincludingfilesinsidesubfolders.
"""
match=fnmatch.fnmatchcaseifcase_sensitiveelsefnmatch.fnmatch
walked=os.walk(folder)ifsubfolderselse[next(os.walk(folder))]
return[os.path.join(root,f)
forroot,dirnames,filenamesinwalked
forfinfilenamesifmatch(f,pattern)]
Share
Improvethisanswer
Follow
answeredDec7,2017at20:10
MarredCheeseMarredCheese
13.3k55goldbadges7575silverbadges7878bronzebadges
Addacomment
|
4
Iwillprovideasampleonelinerwheresourcepathandfiletypecanbeprovidedasinput.Thecodereturnsalistoffilenameswithcsvextension.Use.incaseallfilesneedstobereturned.Thiswillalsorecursivelyscansthesubdirectories.
[yforxinos.walk(sourcePath)foryinglob(os.path.join(x[0],'*.csv'))]
Modifyfileextensionsandsourcepathasneeded.
Share
Improvethisanswer
Follow
editedDec12,2017at5:30
answeredDec11,2017at17:51
VinodhKrishnarajuVinodhKrishnaraju
15711silverbadge77bronzebadges
1
1
Ifyouaregoingtouseglob,thenjustuseglob('**/*.csv',recursive=True).Noneedtocombinethiswithos.walk()torecurse(recursiveand**aresupportedsincePython3.5).
– MartijnPieters
♦
Dec5,2018at11:09
Addacomment
|
3
dircacheis"Deprecatedsinceversion2.6:ThedircachemodulehasbeenremovedinPython3.0."
importdircache
list=dircache.listdir(pathname)
i=0
check=len(list[0])
temp=[]
count=len(list)
whilecount!=0:
iflen(list[i])!=check:
temp.append(list[i-1])
check=len(list[i])
else:
i=i+1
count=count-1
printtemp
Share
Improvethisanswer
Follow
editedMay17,2019at20:30
Georgy
9,83277goldbadges5757silverbadges6666bronzebadges
answeredJul25,2012at10:25
shajishaji
13911silverbadge33bronzebadges
0
Addacomment
|
Nottheansweryou'relookingfor?Browseotherquestionstaggedpythondirectoryoraskyourownquestion.
TheOverflowBlog
Agilitystartswithtrust
WouldyoutrustanAItobeyoureyes?(Ep.437)
FeaturedonMeta
HowmighttheStagingGround&thenewAskWizardworkontheStackExchange...
Shouldweburninatethe[qa]tag?
Overhaulingourcommunity'sclosurereasonsandguidance
Linked
30
Howtogetonlyfilesindirectory?
27
Python3listfilesfromparticulardirectory
15
Readingallfilesinalldirectories
22
python:getlistall*.txtfilesinadirectory
9
HowtolistdirectoryusingPython
6
AutomaticallycreatingalistinPython
7
Howtogetfilepath+filenameintoalist?
5
HowcanIautomaticallyopenalltextfilesinagivenfolder?
6
HowdoiuseLinuxterminalcommandslikeCDandLS?
-6
printthedirectoryforfilesinPython
Seemorelinkedquestions
Related
6256
HowdoImergetwodictionariesinasingleexpression(takeunionofdictionaries)?
5708
HowcanIgetthesourcedirectoryofaBashscriptfromwithinthescriptitself?
6741
WhataremetaclassesinPython?
4931
HowcanIaddablankdirectorytoaGitrepository?
4010
Findingtheindexofaniteminalist
11990
Whatdoesthe"yield"keyworddo?
5164
HowcanIsafelycreateanesteddirectory?
4603
Howtomakeaflatlistoutofalistoflists?
6654
HowdoIfindallfilescontainingspecifictextonLinux?
HotNetworkQuestions
CollatzEncoding
Isauthorshipjustifiedforperforminganexperiment7yearsago,whichispublished?Wejustusedsamplesfromthatexperimentforanewanalysis
RegardingastoryinShivaPurana
Reportpowerifresultisstatisticallysignificant
I’mtryingtomakea220vpowercord
BestWaytoCheckInThreeEventsatOnce?
Wouldahollowedoutworldbemechanicallystable?
CanyoucarrysomeoneusingLevitatelikeaballoon?
Whattypeofmintintzatziki?
NuclearArmageddoncomes!CanBob'splantssurvive?
DuallicensingAGPL3.0/EUPL1.2
Germanyputtingindustriesorfacilities"understatemanagement"iftheydon'tcooperate;provisionsforitintheconstitution?Precedent?
Whyistherea零in三年零九个月?
HowdoIfindtheLaurentseriesexpansionforthiscomplexfunction?
CanamilitarysatellitebeusedtospyonanyplaceonEarthundetected?
Ineedtoexpandamacrodefinedinlistofitemspackage
Arethereinvariantsintextprocessingproblems?
HarmonicfunctionsoncompleteRiemannianmanifolds
VectorsperpendiculartoaBeziercurvepath
Whyisn'ttheRamachandranplotsymmetric?
Whatmakesenergy"the"conservedquantityassociatedwithtemporaltranslationsymmetry?
Myjobbookedmyhotelroomforme.Isitokaytoaskthehotelreceptionisttoaccommodateaspecificroomrequest?
WhatlevelofaudienceshouldoneaimforintheintroductionofaSTEMthesis?
ChevySilveradomufflerand/orexhaustisrattling
morehotquestions
lang-py
Yourprivacy
Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy.
Acceptallcookies
Customizesettings