Get a filtered list of files in a directory - python - Stack Overflow
文章推薦指數: 80 %
If there is no built-in method for this, I am currently thinking of writing a for loop to iterate through the results of an os.listdir() and to ... Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Getafilteredlistoffilesinadirectory AskQuestion Asked 12years,2monthsago Modified 1year,5monthsago Viewed 483ktimes 365 65 IamtryingtogetalistoffilesinadirectoryusingPython,butIdonotwantalistofALLthefiles. WhatIessentiallywantistheabilitytodosomethinglikethefollowingbutusingPythonandnotexecutingls. ls145592*.jpg Ifthereisnobuilt-inmethodforthis,Iamcurrentlythinkingofwritingaforlooptoiteratethroughtheresultsofanos.listdir()andtoappendallthematchingfilestoanewlist. However,therearealotoffilesinthatdirectoryandthereforeIamhopingthereisamoreefficientmethod(orabuilt-inmethod). pythonfilesystemswildcardglobdirectory-listing Share Improvethisquestion Follow editedJan17,2014at23:58 Bart 18.9k77goldbadges6868silverbadges7676bronzebadges askedFeb8,2010at23:02 mhostmhost 6,23044goldbadges3535silverbadges4343bronzebadges 2 [Thislinkmighthelpyou:)Getafilteredlistoffilesinadirectory](codereview.stackexchange.com/a/33642) – sha111 Jan17,2019at10:09 Notethatyoumighttakespecialcareaboutsortingorderifthisisimportantforyourapplication. – lumbric Nov27,2019at20:22 Addacomment | 13Answers 13 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) Helpusimproveouranswers.Aretheanswersbelowsortedinawaythatputsthebestansweratornearthetop? Takeashortsurvey I’mnotinterested 501 importglob jpgFilenamesList=glob.glob('145592*.jpg') Seeglobinpythondocumenttion Share Improvethisanswer Follow editedJul14,2020at12:02 MatteoRagni 2,68911goldbadge2020silverbadges3131bronzebadges answeredFeb8,2010at23:05 IgnacioVazquez-AbramsIgnacioVazquez-Abrams 737k145145goldbadges12941294silverbadges13251325bronzebadges 12 24 Oh,IjustnoticedthatthePythondocssayglob()"isdonebyusingtheos.listdir()andfnmatch.fnmatch()functionsinconcert,andnotbyactuallyinvokingasubshell".Inotherwords,glob()doesn'thavetheefficiencyimprovementsonemightexpect. – BenHoyt Feb11,2010at4:28 6 Thereisonemaindifference:glob.glob('145592*.jpg')printsthewholeabsolutepathoffileswhilels145592*.jpgprintsonlythelistoffiles. – ÉbeIsaac Dec2,2016at18:12 8 @BenWhywouldinvokingasubshell(subprocess)haveanyefficiencyimprovements? – PauloNeves Jan18,2017at8:08 7 @PauloNeves:true,mycommentabovedoesn'tmakesensetome7yearslatereither.:-)I'mguessingIwasreferringtothefactthatglob()justuseslistdir+fnmatch,ratherthanspecialoperatingsystemcallstodothewildcardfiltering.Forexample,onWindowstheFindFirstFileAPIallowsyoutospecifywildcardssotheOSdoesthefilteringdirectly,andpresumablymoreefficiently(Idon'tthinkthere'sanequivalentonLinux). – BenHoyt Jan18,2017at16:28 2 Don'tforgettouseimportglob – Sky Mar19,2020at23:29 | Show7morecomments 157 glob.glob()isdefinitelythewaytodoit(asperIgnacio).However,ifyoudoneedmorecomplicatedmatching,youcandoitwithalistcomprehensionandre.match(),somethinglikeso: files=[fforfinos.listdir('.')ifre.match(r'[0-9]+.*\.jpg',f)] Moreflexible,butasyounote,lessefficient. Share Improvethisanswer Follow answeredFeb9,2010at0:27 BenHoytBenHoyt 9,76644goldbadges5656silverbadges8484bronzebadges 2 1 Thisdefinitelyseemstobemorepowerful.Forexample,havingtodosomethinglike[0-9]+ – demongolem Jan10,2013at17:03 3 Yes,definitelymorepowerful--howeverfnmatchdoessupport[0123456789]sequences(seedocs),anditalsohasthefnmatch.filter()functionwhichmakesthisloopslightlymoreefficient. – BenHoyt Jan10,2013at22:42 Addacomment | 74 Keepitsimple: importos relevant_path="[pathtofolder]" included_extensions=['jpg','jpeg','bmp','png','gif'] file_names=[fnforfninos.listdir(relevant_path) ifany(fn.endswith(ext)forextinincluded_extensions)] IpreferthisformoflistcomprehensionsbecauseitreadswellinEnglish. Ireadthefourthlineas: Foreachfninos.listdirformypath,givemeonlytheonesthatmatchanyoneofmyincludedextensions. Itmaybehardfornovicepythonprogrammerstoreallygetusedtousinglistcomprehensionsforfiltering,anditcanhavesomememoryoverheadforverylargedatasets,butforlistingadirectoryandothersimplestringfilteringtasks,listcomprehensionsleadtomorecleandocumentablecode. Theonlythingaboutthisdesignisthatitdoesn'tprotectyouagainstmakingthemistakeofpassingastringinsteadofalist.Forexampleifyouaccidentallyconvertastringtoalistandendupcheckingagainstallthecharactersofastring,youcouldendupgettingaslewoffalsepositives. Butit'sbettertohaveaproblemthat'seasytofixthanasolutionthat'shardtounderstand. Share Improvethisanswer Follow editedDec18,2018at7:46 AlexMontoya 3,91311goldbadge2525silverbadges2727bronzebadges answeredJan13,2014at16:27 ramsey0ramsey0 1,44711goldbadge1212silverbadges1010bronzebadges 3 6 Notthatthereisanyneedforany()here,becausestr.endswith()takesasequenceofendings.iffn.endswith(included_extentensions)ismorethanenough. – MartijnPieters ♦ Oct8,2015at7:10 3 Apartfromtheinefficiencyofnotusingstr.endswith(seq)thatMartijnpointedout,thisisnotcorrect,becauseafilehastoendwith.extforittohavethatextension.Thiscodewillalsofind(forexample)afilecalled"myjpg"oradirectorynamedjust"png".Tofix,justprefixeachextensioninincluded_extensionswitha.. – BenHoyt Nov3,2016at19:29 I'malwaysabitwaryofcodeinanswerswhichobviouslyhasn'tbeenrunorcan'trun.Thevariableincluded_extensionsvsincluded_extentsions?Apitybecauseotherwisethisismypreferredanswer. – Auspice Mar19,2018at21:35 Addacomment | 53 Anotheroption: >>>importos,fnmatch >>>fnmatch.filter(os.listdir('.'),'*.py') ['manage.py'] https://docs.python.org/3/library/fnmatch.html Share Improvethisanswer Follow editedFeb5,2017at0:12 Matt 25.9k66goldbadges7878silverbadges7373bronzebadges answeredJan28,2016at11:55 RisadinhaRisadinha 14.5k22goldbadges7878silverbadges8787bronzebadges 3 7 Thisisexactlywhatglobdoesonasingleline. – ItayGrudev Apr12,2016at15:14 1 Onlydifferenceisglobreturnsthefullpathasopposedtoos.listdirjustreturningthefilename.AtleastthisiswhatishappeninginPython2. – KarthicRaghupathi May1,2019at19:41 Averynicesolution.Especiallyforthosewhoarealreadyusingfnmatchandosintheirscriptanddon'twanttoimportanothermoduleie.glob. – TheoF Jan4at12:38 Addacomment | 32 Filterwithglobmodule: Importglob importglob WildCards: files=glob.glob("data/*") print(files) Out: ['data/ks_10000_0','data/ks_1000_0','data/ks_100_0','data/ks_100_1', 'data/ks_100_2','data/ks_106_0','data/ks_19_0','data/ks_200_0','data/ks_200_1', 'data/ks_300_0','data/ks_30_0','data/ks_400_0','data/ks_40_0','data/ks_45_0', 'data/ks_4_0','data/ks_500_0','data/ks_50_0','data/ks_50_1','data/ks_60_0', 'data/ks_82_0','data/ks_lecture_dp_1','data/ks_lecture_dp_2'] Fiterextension.txt: files=glob.glob("/home/ach/*/*.txt") Asinglecharacter glob.glob("/home/ach/file?.txt") NumberRanges glob.glob("/home/ach/*[0-9]*") AlphabetRanges glob.glob("/home/ach/[a-c]*") Share Improvethisanswer Follow answeredMar31,2019at9:00 pink.slashpink.slash 1,6071414silverbadges1313bronzebadges Addacomment | 15 Preliminarycode importglob importfnmatch importpathlib importos pattern='*.py' path='.' Solution1-use"glob" #lookupincurrentdir glob.glob(pattern) In[2]:glob.glob(pattern) Out[2]:['wsgi.py','manage.py','tasks.py'] Solution2-use"os"+"fnmatch" Variant2.1-Lookupincurrentdir #lookupincurrentdir fnmatch.filter(os.listdir(path),pattern) In[3]:fnmatch.filter(os.listdir(path),pattern) Out[3]:['wsgi.py','manage.py','tasks.py'] Variant2.2-Lookuprecursive #lookuprecursive fordirpath,dirnames,filenamesinos.walk(path): ifnotfilenames: continue pythonic_files=fnmatch.filter(filenames,pattern) ifpythonic_files: forfileinpythonic_files: print('{}/{}'.format(dirpath,file)) Result ./wsgi.py ./manage.py ./tasks.py ./temp/temp.py ./apps/diaries/urls.py ./apps/diaries/signals.py ./apps/diaries/actions.py ./apps/diaries/querysets.py ./apps/library/tests/test_forms.py ./apps/library/migrations/0001_initial.py ./apps/polls/views.py ./apps/polls/formsets.py ./apps/polls/reports.py ./apps/polls/admin.py Solution3-use"pathlib" #lookupincurrentdir path_=pathlib.Path('.') tuple(path_.glob(pattern)) #lookuprecursive tuple(path_.rglob(pattern)) Notes: TestedonthePython3.4 Themodule"pathlib"wasaddedonlyinthePython3.4 ThePython3.5addedafeatureforrecursivelookupwithglob.glob https://docs.python.org/3.5/library/glob.html#glob.glob.SincemymachineisinstalledwithPython3.4,Ihavenottestedthat. Share Improvethisanswer Follow editedApr21,2018at12:49 S0AndS0 80811goldbadge77silverbadges1919bronzebadges answeredNov12,2016at19:32 PADYMKOPADYMKO 3,73922goldbadges3333silverbadges3636bronzebadges Addacomment | 10 useos.walktorecursivelylistyourfiles importos root="/home" pattern="145992" alist_filter=['jpg','bmp','png','gif'] path=os.path.join(root,"mydir_to_scan") forr,d,finos.walk(path): forfileinf: iffile[-3:]inalist_filterandpatterninfile: printos.path.join(root,file) Share Improvethisanswer Follow editedAug26,2013at16:41 CommunityBot 111silverbadge answeredFeb9,2010at1:46 ghostdog74ghostdog74 306k5555goldbadges249249silverbadges337337bronzebadges 2 Noneedtoslice;file.endswith(alist_filter)isenough. – MartijnPieters ♦ Oct8,2015at7:10 Wehavetouseany(file.endswith(filter)forfilterinalist_filter)asendswith()doesnotallowlistasaparameter. – avnihirpara Nov9,2020at3:34 Addacomment | 6 YoucanusepathlibthatisavailableinPythonstandardlibrary3.4andabove. frompathlibimportPath files=[fforfinPath.cwd().iterdir()iff.match("145592*.jpg")] Share Improvethisanswer Follow answeredSep17,2019at19:55 VladBezdenVladBezden 71.7k2222goldbadges231231silverbadges167167bronzebadges 1 1 Alternatively,justusePath.cwd().glob("145592*.jpg")...Anywaythisshoulddefinitelybehigheronthispage.pathlibisthewaygo – Tomerikoo May5,2021at18:33 Addacomment | 5 importos dir="/path/to/dir" [x[0]+"/"+fforxinos.walk(dir)forfinx[2]iff.endswith(".jpg")] Thiswillgiveyoualistofjpgfileswiththeirfullpath.Youcanreplacex[0]+"/"+fwithfforjustfilenames.Youcanalsoreplacef.endswith(".jpg")withwhateverstringconditionyouwish. Share Improvethisanswer Follow answeredNov19,2016at13:47 EvgenijM86EvgenijM86 2,02911goldbadge1717silverbadges1010bronzebadges Addacomment | 4 youmightalsolikeamorehigh-levelapproach(Ihaveimplementedandpackagedasfindtools): fromfindtools.find_filesimport(find_files,Match) #Recursivelyfindall*.txtfilesin**/home/** txt_files_pattern=Match(filetype='f',name='*.txt') found_files=find_files(path='/home',match=txt_files_pattern) forfound_fileinfound_files: printfound_file canbeinstalledwith pipinstallfindtools Share Improvethisanswer Follow answeredMay29,2014at22:13 YauhenYakimovichYauhenYakimovich 12.9k77goldbadges5656silverbadges6565bronzebadges Addacomment | 2 Filenameswith"jpg"and"png"extensionsin"path/to/images": importos accepted_extensions=["jpg","png"] filenames=[fnforfninos.listdir("path/to/images")iffn.split(".")[-1]inaccepted_extensions] Share Improvethisanswer Follow answeredMar22,2018at18:38 gypsygypsy 2111bronzebadge 1 1 Thisisverysimilartotheanswergivenby@ramsey0 – chb Mar22,2018at19:03 Addacomment | 1 Youcandefinepatternandcheckforit.HereIhavetakenbothstartandendpatternandlookingfortheminthefilename.FILEScontainsthelistofallthefilesinadirectory. importos PATTERN_START="145592" PATTERN_END=".jpg" CURRENT_DIR=os.path.dirname(os.path.realpath(__file__)) forr,d,FILESinos.walk(CURRENT_DIR): forFILEinFILES: ifPATTERN_STARTinFILE.startwith(PATTERN_START)andPATTERN_ENDinFILE.endswith(PATTERN_END): printFILE Share Improvethisanswer Follow editedNov9,2020at3:44 answeredNov14,2019at5:42 RishiBansalRishiBansal 3,18122goldbadges2222silverbadges4242bronzebadges 2 1 PATTERN_STARTshouldbeusedasFILE.startwith(PATTERN_START)andPATTERN_ENDshouldbeusedasFILE.endswith(PATTERN_END)toavoidanyotherfilenamecombination.Forexampleabovecodewillallowjpg_sample_145592filealso.Whichisnotcorrect. – avnihirpara Nov9,2020at3:42 IthinkitshouldbeifFILE.startwith(PATTERN_START)andFILE.endswith(PATTERN_END): – avnihirpara Nov9,2020at4:15 Addacomment | -3 Youcanusesubprocess.check_ouput()as importsubprocess list_files=subprocess.check_output("ls145992*.jpg",shell=True) Ofcourse,thestringbetweenquotescanbeanythingyouwanttoexecuteintheshell,andstoretheoutput. Share Improvethisanswer Follow answeredSep28,2016at13:08 DavidA.DavidA. 9 1 Onlyoneproblem.ls'soutputshouldnotbeparsed. – ivan_pozdeev Sep29,2016at13:22 Addacomment | YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedpythonfilesystemswildcardglobdirectory-listingoraskyourownquestion. TheOverflowBlog Agilitystartswithtrust WouldyoutrustanAItobeyoureyes?(Ep.437) FeaturedonMeta HowmighttheStagingGround&thenewAskWizardworkontheStackExchange... Overhaulingourcommunity'sclosurereasonsandguidance Visitchat Linked 1 Findinganddeletingfilesusingpythonscript 1 UsingPython,howdoyouremovefilesgivenafilespecsuchas"*.obj"onWindows? 0 Howtolistoutfilenamesinadirectorybasedonapattern 0 TryingtorenamemultiplefilesusingOSmodule(Python),gettingerrormessage:"ValueError:notenoughvaluestounpack(expected2,got1)" 1 Removingfilesfromadirectorymatchingapattern 0 Usingfilesindirectorythatarematchingwithalist 2 Howtoimportafilefromafolderwheretheendingcharacterscanchange-pythonpandas? 2 Filteringonlyfilesinaparticularextension-Python 2 Howtousebashcommandresultsaspythonargs? 0 HowtogetlistfilesmatchingnameinPython Seemorelinkedquestions Related 3232 HowdoIcheckifalistisempty? 4010 Findingtheindexofaniteminalist 5164 HowcanIsafelycreateanesteddirectory? 2560 HowdoIgetthelastelementofalist? 4603 Howtomakeaflatlistoutofalistoflists? 812 Gettingalistofallsubdirectoriesinthecurrentdirectory 1398 HowdoyougetalistofthenamesofallfilespresentinadirectoryinNode.js? 3467 HowdoIlistallfilesofadirectory? 707 HowdoIappendonestringtoanotherinPython? HotNetworkQuestions Howtime-symmetricisorbitalmechanics? AccordingtoTrinitarians,whenthetermYahweh('LORD')isusedintheOT,whoexactlyisbeingreferredto? UsingChineseinatable Copying|0⟩,|1⟩Qubitswillbreaktheno-cloningtheorem? Whatarebestpracticesforbuildingadedicatedspaceformathematicsmajors? Whyisfastmatrixmultiplicationimpractical? HowdidJesusgrowinfavorwithGod?Luke2:52 IsthereanidiomaticFrenchexpressionfor"Theregoesmy/your/etc....,"meaningsomethingyouassumedyouhadsuddenlydisappears? DifferencebetweenWesternsanctionsandRussia'schoicetostopoil&gas Howmanymagicworkoutsecretsaretherereally? Whyisn'ttheRamachandranplotsymmetric? Smartwaytoremoverulesfromlists,leavingjustvalues,specificallyfromFindMinimumoutput usageof\greadlist*frompackagelistofitems Myjobbookedmyhotelroomforme.Isitokaytoaskthehotelreceptionisttoaccommodateaspecificroomrequest? Sailinganage-of-sailsloopwith2-mancrew Techniquesfordebuggingproofs Isresponsibilityforachildbasedongenetics? Strangeshopping Whyspecifyamaximumpulsewidthforresetpin? DoyouneedaHilbertstyleEpsilonoperatorfordefinitionsinsettheory? Wheredoestherootoftrustactuallylie? Howdoesonedeterminetheboundaryofanobject? WhydophysicalistsusetheTuringMachineasamodelofthebrain? IsSchedule80PVCpiperecommended/commonlyusedforwatermainconnections? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1用Python 定位特定類型文件 - 程式人生
import os >>> [txt for txt in os.listdir('.') if txt.endswith('.txt')] ['b.txt', ... filter:返回輸入列...
- 2List All Files in A Directory with Python Guideline - Holistic SEO
- 3python os.listdir filter Code Example - Grepper
Python answers related to “python os.listdir filter”. pydrive list folders · list files in direct...
- 4Get a filtered list of files in a directory - python - Stack Overflow
If there is no built-in method for this, I am currently thinking of writing a for loop to iterate...
- 5How to List Files in a Directory Using Python? - AskPython
files = [f for f in os.listdir(path) if os.path.isfile(f)] ... In the above code snippet, List Co...