Walking — PyFilesystem 2.4.15 documentation
文章推薦指數: 80 %
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.
延伸文章資訊
- 1A file walker inspired by Python's os.walk() - GitHub
Walker is a file system walker, i.e. it lets you traverse a directory recursively listing its fil...
- 2Python os.walk() Method - Tutorialspoint
Description. Python method walk() generates the file names in a directory tree by walking the tre...
- 3os.walk() in Python - GeeksforGeeks
OS.walk() generate the file names in a directory tree by walking the tree either top-down or bott...
- 4Python os.walk() 方法 - 菜鸟教程
Python os.walk() 方法. Python File(文件) 方法 Python OS 文件/目录方法. 概述. os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,...
- 5適用於Python的Directory Walker - 程式人生
另外,我建議使用“file”以外的詞,因為它在python語言中有某種含義。但不是關鍵字,所以它仍在執行。 順便說一下,在處理檔名時,我發現OS.PATH模組非常有用,我建議您仔細 ...