Get a filtered list of files in a directory - Python - 开源软件问题中文版 ...

文章推薦指數: 80 %
投票人數:10人

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 append all the ... 编程技术网'sArchiver 问题›Python›Getafilteredlistoffilesinadirectory AngeloMarcotu 发表于2022-1-100:32:50 Getafilteredlistoffilesinadirectory GetafilteredlistoffilesinadirectoryIamtryingtogetalistoffilesinadirectoryusingPython,butIdonotwantalistofALLthefiles. WhatIessentiallywantistheabilitytodosomethinglikethefollowingbutusingPythonandnotexecutingls. ls145592*.jpg Ifthereisnobuilt-inmethodforthis,Iamcurrentlythinkingofwritingaforlooptoiteratethroughtheresultsofanos.listdir()andtoappendallthematchingfilestoanewlist. However,therearealotoffilesinthatdirectoryandthereforeIamhopingthereisamoreefficientmethod(orabuilt-inmethod). debasishg 发表于2022-1-101:05:55 glob.glob()isdefinitelythewaytodoit(asperIgnacio).However,ifyoudoneedmorecomplicatedmatching,youcandoitwithalistcomprehensionandre.match(),somethinglikeso: files=[fforfinos.listdir('.')ifre.match(r'[0-9]+.*\.jpg',f)] Moreflexible,butasyounote,lessefficient. HASH 发表于2022-1-101:32:38 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. PoojaKatkade 发表于2022-1-102:26:57 Anotheroption: >>>importos,fnmatch >>>fnmatch.filter(os.listdir('.'),'*.py') ['manage.py'] https://docs.python.org/3/library/fnmatch.html JINURAJ 发表于2022-1-102:01:23 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]*") SantoshKhalse 发表于2022-1-101:27:04 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.globhttps://docs.python.org/3.5/library/glob.html#glob.glob.SincemymachineisinstalledwithPython3.4,Ihavenottestedthat. cabonamigo 发表于2022-1-101:21:14 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) kushagramitta 发表于2022-1-100:40:04 YoucanusepathlibthatisavailableinPythonstandardlibrary3.4andabove. frompathlibimportPath files=[fforfinPath.cwd().iterdir()iff.match("145592*.jpg")] boofhead 发表于2022-1-101:32:56 importos [x[0]+"/"+fforxinos.walk(dir)forfinx[2]iff.endswith(".jpg")] Thiswillgiveyoualistofjpgfileswiththeirfullpath.Youcanreplacex[0]+"/"+fwithfforjustfilenames.Youcanalsoreplacef.endswith(".jpg")withwhateverstringconditionyouwish. NullPointerExc 发表于2022-1-101:22:28 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 页: [1] 2 查看完整版本: Getafilteredlistoffilesinadirectory Poweredbywww.editcode.netX3.4Archiver Copyright&copy2001-2021编程技术网.



請為這篇文章評分?