Python os.walk() Method - Tutorialspoint

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

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



請為這篇文章評分?