Walking — PyFilesystem 2.4.15 documentation

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

Here's an example that prints the path to every Python file in your projects directory: >>> from fs import open_fs >>> from fs.walk import Walker ... PyFilesystem latest Introduction Guide Concepts ResourceInfo FSURLs Walking WalkMethods SearchAlgorithms Globbing BuiltinFilesystems ImplementingFilesystems Creatinganextension ExternalFilesystems PyFilesystemAPI Reference ContributingtoPyFilesystem PyFilesystem Docs» Walking EditonGitHub Walking¶ Walkingafilesystemmeansrecursivelyvisitingadirectoryandanysub-directories.Itisafairlycommonrequirementforcopying,searchingetc. Towalkafilesystem(ordirectory)youcanconstructaWalkerobjectanduseitsmethodstodothewalking.Here’sanexamplethatprintsthepathtoeveryPythonfileinyourprojectsdirectory: >>>fromfsimportopen_fs >>>fromfs.walkimportWalker >>>home_fs=open_fs('~/projects') >>>walker=Walker(filter=['*.py']) >>>forpathinwalker.files(home_fs): ...print(path) Generallyspeaking,however,youwillonlyneedtoconstructaWalkerobjectifyouwanttocustomizesomebehaviorofthewalkingalgorithm.ThisisbecauseyoucanaccessthefunctionalityofaWalkerobjectviathewalkattributeonFSobjects.Here’sanexample: >>>fromfsimportopen_fs >>>home_fs=open_fs('~/projects') >>>forpathinhome_fs.walk.files(filter=['*.py']): ...print(path) Notethatthefilesmethodabovedoesn’trequireafsparameter.ThisisbecausethewalkattributeisapropertywhichreturnsaBoundWalkerobject,whichassociatesthefilesystemwithawalker. WalkMethods¶ IfyoucallthewalkattributeonaBoundWalkeritwillreturnaniterableofStepnamedtupleswiththreevalues;apathtothedirectory,alistofInfoobjectsfordirectories,andalistofInfoobjectsforthefiles.Here’sanexample: forstepinhome_fs.walk(filter=['*.py']): print('Indir{}'.format(step.path)) print('sub-directories:{!r}'.format(step.dirs)) print('files:{!r}'.format(step.files)) Note MethodsofBoundWalkerinvokeacorrespondingmethodonaWalkerobject,withtheboundfilesystem. Thewalkattributemayappeartobeamethod,butisinfactacallableobject.Itsupportsotherconvenientmethodsthatsupplydifferentinformationfromthewalk.Forinstance,files(),whichreturnsaniterableoffilepaths.Here’sanexample: forpathinhome_fs.walk.files(filter=['*.py']): print('Pythonfile:{}'.format(path)) Thecomplementtofilesisdirs()whichreturnspathstojustthedirectories(andignoringthefiles).Here’sanexample: fordir_pathinhome_fs.walk.dirs(): print("{!r}containssub-directory{}".format(home_fs,dir_path)) Theinfo()methodreturnsageneratoroftuplescontainingapathandanInfoobject.Youcanusetheis_dirattributetoknowifthepathreferstoadirectoryorfile.Here’sanexample: forpath,infoinhome_fs.walk.info(): ifinfo.is_dir: print("[dir]{}".format(path)) else: print("[file]{}".format(path)) Finally,here’saniceexamplethatcountsthenumberofbytesofPythoncodeinyourhomedirectory: bytes_of_python=sum( info.size forinfoinhome_fs.walk.info(namespaces=['details']) ifnotinfo.is_dir ) SearchAlgorithms¶ Therearetwogeneralalgorithmsforsearchingadirectorytree.Thefirstmethodis"breadth",whichyieldsresourcesinthetopofthedirectorytreefirst,beforemovingontosub-directories.Thesecondis"depth"whichyieldsthemostdeeplynestedresources,andworksbackwardstothetop-mostdirectory. Generallyspeaking,youwillonlyneedtheadepthsearchifyouwillbedeletingresourcesasyouwalkthroughthem.Thedefaultbreadthsearchisagenerallymoreefficientwayoflookingthroughafilesystem.YoucanspecifywhichmethodyouwantwiththesearchparameteronmostWalkermethods. ReadtheDocs v:latest Versions latest stable v2.4.15 v2.4.14 v2.4.13 v2.4.12 v2.4.11 v2.4.10 v2.4.9 v2.4.8 v2.4.7 v2.4.6 v2.4.5 v2.4.4 v2.4.3 v2.4.2 v2.4.1 v2.4.0 v2.3.1 v2.3.0 v2.2.1 v2.2.0 v2.1.3 v2.1.2 v2.1.1 v2.1.0 v2.0.27 v2.0.26 v2.0.25 v2.0.24 v2.0.23 v2.0.21 v2.0.20 v2.0.19 v2.0.18 v2.0.17 v2.0.16 v2.0.15 v2.0.13 v2.0.12 v2.0.11 v2.0.9 Downloads pdf html epub OnReadtheDocs ProjectHome Builds FreedocumenthostingprovidedbyReadtheDocs.



請為這篇文章評分?