How to list files in a directory in Python

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

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 Theglobmodulealsomakesitpossible​togetalistoffilesorfoldersinadirectory. 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.



請為這篇文章評分?