Python os.walk() Method - Tutorialspoint
文章推薦指數: 80 %
Description. Python method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. PythonBasicTutorial Python-Home Python-Overview Python-EnvironmentSetup Python-BasicSyntax Python-VariableTypes Python-BasicOperators Python-DecisionMaking Python-Loops Python-Numbers Python-Strings Python-Lists Python-Tuples Python-Dictionary Python-Date&Time Python-Functions Python-Modules Python-FilesI/O Python-Exceptions PythonAdvancedTutorial Python-Classes/Objects Python-RegExpressions Python-CGIProgramming Python-DatabaseAccess Python-Networking Python-SendingEmail Python-Multithreading Python-XMLProcessing Python-GUIProgramming Python-FurtherExtensions PythonUsefulResources Python-QuestionsandAnswers Python-QuickGuide Python-Tools/Utilities Python-UsefulResources Python-Discussion SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Pythonos.walk()Method Advertisements PreviousPage NextPage Description Pythonmethodwalk()generatesthefilenamesinadirectorytreebywalkingthetreeeithertop-downorbottom-up. Syntax Followingisthesyntaxforwalk()method− os.walk(top[,topdown=True[,onerror=None[,followlinks=False]]]) Parameters top−Eachdirectoryrootedatdirectory,yields3-tuples,i.e.,(dirpath,dirnames,filenames) topdown−IfoptionalargumenttopdownisTrueornotspecified,directoriesarescannedfromtop-down.IftopdownissettoFalse,directoriesarescannedfrombottom-up. onerror−Thiscanshowerrortocontinuewiththewalk,orraisetheexceptiontoabortthewalk. followlinks−Thisvisitsdirectoriespointedtobysymlinks,ifsettotrue. ReturnValue Thismethodreturnsvalue. Example Thefollowingexampleshowstheusageofwalk()method. #!/usr/bin/python importos forroot,dirs,filesinos.walk(".",topdown=False): fornameinfiles: print(os.path.join(root,name)) fornameindirs: print(os.path.join(root,name)) Letuscompileandruntheaboveprogram,thiswillscanallthedirectoriesandsubdirectoriesbottom-to-up ./tmp/test.py ./.bash_logout ./amrood.tar.gz ./.emacs ./httpd.conf ./www.tar.gz ./mysql.tar.gz ./test.py ./.bashrc ./.bash_history ./.bash_profile ./tmp IfyouwillchangethevalueoftopdowntoTrue,thenitwillgiveyouthefollowingresult− ./.bash_logout ./amrood.tar.gz ./.emacs ./httpd.conf ./www.tar.gz ./mysql.tar.gz ./test.py ./.bashrc ./.bash_history ./.bash_profile ./tmp ./tmp/test.py python_files_io.htm UsefulVideoCourses Video PythonOnlineTraining 187Lectures 17.5hours MalharLathkar MoreDetail Video PythonEssentialsOnlineTraining 55Lectures 8hours ArnabChakraborty MoreDetail Video LearnPythonProgrammingin100EasySteps 136Lectures 11hours In28MinutesOfficial MoreDetail Video PythonwithDataScience BestSeller 75Lectures 13hours EduonixLearningSolutions MoreDetail Video Python3fromscratchtobecomeadeveloperindemand BestSeller 70Lectures 8.5hours LetsKodeIt MoreDetail Video PythonDataSciencebasicswithNumpy,PandasandMatplotlib MostPopular 63Lectures 6hours AbhilashNelson MoreDetail PreviousPage PrintPage NextPage Advertisements Print AddNotes Bookmarkthispage ReportError Suggestions Save Close Dashboard Logout
延伸文章資訊
- 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() 遞迴印出資料夾中所有目錄及檔名
短短幾行就能達成我的目的,且python 的程式碼也能在windows 上運作。 以下是能達成我目的的 Python walk.py 原始碼: import os for dirPath, di...
- 3fs.walk — PyFilesystem 2.4.15 documentation
An iterator over file paths (absolute from the filesystem root). Return type: Iterator. This meth...
- 4Walking — PyFilesystem 2.4.15 documentation
Here's an example that prints the path to every Python file in your projects directory: >>> from ...
- 5os — Miscellaneous operating system interfaces — Python ...
In Python, file names, command line arguments, and environment variables are represented using th...