How to list files in a directory in Python
文章推薦指數: 80 %
Python's os module provides a function that gets a list of files or folders in a directory. The . , which is passed as an argument to os.listdir() ... SolutionsEducativeEnterpriseEnablementplatformDevelopersLearnnewtechnologiesProductsCoursesforEnterpriseSuperchargeyourengineeringteamCoursesforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsPricingForIndividualsStayaheadofthecurveForEnterpriseTailoredforyourteamCoursesLogInJoinforfreeaconciseshotofdevknowledgeBecomeaContributorConciseshotsofdevknowledgeRELATEDTAGSHowtolistfilesinadirectoryinPythonInPython,wemaywantalistoffilesorfoldersinaspecifieddirectory.Thereareseveralmethodsthatcanbeusedtodothis. 1.Usingtheosmodule Python’sosmoduleprovidesafunctionthatgetsalistoffilesorfoldersinadirectory.The.,whichispassedasanargumenttoos.listdir(),signifiesthecurrentfolder. main.pyfile3.pyfile2.csvfile1.txtimportos arr=os.listdir('.') print(arr) RunTolistfilesataspecificpath,wecansimplygivethepathasastringtothefunction. ThispathwillhavetoberelativetowhereyourPythonfileisplacedor,ifyou’renotworkingwithfiles,thepathwillberelativetowhereyourPythonShellhasbeenlaunched: os.listdir('My_Downloads/Music') 2.Usingtheglobmodule Theglobmodulealsomakesitpossibletogetalistoffilesorfoldersinadirectory. main.pyfile3.pyfile2.csvfile1.txtimportglob print(glob.glob('.'))RunWecanalsoprintthefilenamesrecursivelyusingtheiglobmethod.Inthemethodcall,therecursiveparametermustbesettoTrue. forfile_nameinglob.iglob('Desktop/**/*.txt',recursive=True): print(file_name) ThecodeabovewillsearchtheDesktopfolderrecursivelyandprintall.txtfiles.Wecanreplace*.txtwithonlya*toprintallfiles. The**commandisusedtosearchrecursivelyandisonlyapplicablewhentherecursiveparameterisTrue. RELATEDCOURSESViewallCoursesKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForIndividualsForEnterpriseRESOURCESEducativeBlogEdpressoCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceABOUTUSOurTeamCareersHiringMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPricingForIndividualsForEnterpriseLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServicePRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringRESOURCESEducativeBlogEdpressoMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsRESOURCESEducativeBlogEdpressoCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForIndividualsForEnterpriseLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsCopyright©2022Educative,Inc.Allrightsreserved.
延伸文章資訊
- 1How do I list all files of a directory? - python - Stack Overflow
The listdir() method returns the list of entries for the given directory. The method os.path.isfi...
- 2How To List Files In Directory Python- Detailed Guide - Stack ...
os.walk() method can be used to list the files and folders in a directory.
- 3How to list files in a directory in Python
- 4Python - List Files in a Directory - ItsMyCode
In Python we can List Files in a Directory using the os module which has built-in methods like os...
- 5Create A List Of All Files In A Folder - College of Agriculture ...