Python | os.path.dirname() method - GeeksforGeeks

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

os.path.dirname() method in Python is used to get the directory name from the specified path. Syntax: os.path.dirname( ... Skiptocontent CoursesForWorkingProfessionalsLIVEDSALiveClassesSystemDesignJavaBackendDevelopmentFullStackLIVEExploreMoreSelf-PacedDSA-SelfPacedSDETheoryMust-DoCodingQuestionsExploreMoreForStudentsLIVECompetitiveProgrammingDataStructureswithC++DataScienceExploreMoreSelf-PacedDSA-SelfPacedCIPJAVA/Python/C++ExploreMoreSchoolCoursesSchoolGuidePythonProgrammingLearnToMakeAppsExploremoreAllCoursesTutorialsPracticeDS&Algo.MustDoQuestionsDSATopic-wiseDSACompany-wiseAlgorithmsAnalysisofAlgorithmsAsymptoticAnalysisWorst,AverageandBestCasesAsymptoticNotationsLittleoandlittleomeganotationsLowerandUpperBoundTheoryAnalysisofLoopsSolvingRecurrencesAmortizedAnalysisWhatdoes'SpaceComplexity'mean?Pseudo-polynomialAlgorithmsPolynomialTimeApproximationSchemeATimeComplexityQuestionSearchingAlgorithmsSortingAlgorithmsGraphAlgorithmsPatternSearchingGeometricAlgorithmsMathematicalBitwiseAlgorithmsRandomizedAlgorithmsGreedyAlgorithmsDynamicProgrammingDivideandConquerBacktrackingBranchandBoundAllAlgorithmsDataStructuresArraysLinkedListStackQueueBinaryTreeBinarySearchTreeHeapHashingGraphAdvancedDataStructureMatrixStringsAllDataStructuresInterviewCornerCompanyPreparationTopTopicsPracticeCompanyQuestionsInterviewExperiencesExperiencedInterviewsInternshipInterviewsCompetititveProgrammingDesignPatternsSystemDesignTutorialMultipleChoiceQuizzesLanguagesCC++JavaPythonC#JavaScriptjQuerySQLPHPScalaPerlGoLanguageHTMLCSSKotlinML&DataScienceMachineLearningDataScienceCSSubjectsMathematicsOperatingSystemDBMSComputerNetworksComputerOrganizationandArchitectureTheoryofComputationCompilerDesignDigitalLogicSoftwareEngineeringGATEGATEComputerScienceNotesLastMinuteNotesGATECSSolvedPapersGATECSOriginalPapersandOfficialKeysGATE2021DatesGATECS2021SyllabusImportantTopicsforGATECSWebTechnologiesHTMLCSSJavaScriptAngularJSReactJSNodeJSBootstrapjQueryPHPSoftwareDesignsSoftwareDesignPatternsSystemDesignTutorialSchoolLearningSchoolProgrammingMathematicsNumberSystemAlgebraTrigonometryStatisticsProbabilityGeometryMensurationCalculusMathsNotes(Class8-12)Class8NotesClass9NotesClass10NotesClass11NotesClass12NotesNCERTSolutionsClass8MathsSolutionClass9MathsSolutionClass10MathsSolutionClass11MathsSolutionClass12MathsSolutionRDSharmaSolutionsClass8MathsSolutionClass9MathsSolutionClass10MathsSolutionClass11MathsSolutionClass12MathsSolutionPhysicsNotes(Class8-11)Class8NotesClass9NotesClass10NotesClass11NotesCSExams/PSUsISROISROCSOriginalPapersandOfficialKeysISROCSSolvedPapersISROCSSyllabusforScientist/EngineerExamUGCNETUGCNETCSNotesPaperIIUGCNETCSNotesPaperIIIUGCNETCSSolvedPapersStudentCampusAmbassadorProgramSchoolAmbassadorProgramProjectGeekoftheMonthCampusGeekoftheMonthPlacementCourseCompetititveProgrammingTestimonialsGeekontheTopCareersInternshipJobsApplyforJobsPostaJobJOB-A-THONEvents ComewritearticlesforusandgetfeaturedLearnandcodewiththebestindustryexpertsGetaccesstoad-freecontent,doubtassistanceandmore!ComeandfindyourdreamjobwithusGeeksDigestQuizzesGeeksCampusGblogArticlesIDECampusMantriHomeSavedVideosCoursesGBlogPuzzlesWhat'sNew? ChangeLanguage DataStructuresAlgorithmsInterviewPreparationTopic-wisePracticeC++JavaPythonCompetitiveProgrammingMachineLearningHTMLSDESheetPuzzlesGFGSchoolProjects RelatedArticles ▲RelatedArticlesPython|os.path.dirname()methodPython|os.path.join()methodOSPathmoduleinPythonOSModuleinPythonwithExamplesCreateadirectoryinPythonPython:CheckifaFileorDirectoryExistsPython|os.mkdir()methodPython|os.makedirs()methodPython|os.path.exists()methodPython|os.path.isfile()methodPython|os.path.isdir()methodPython|os.listdir()methodos.walk()inPythonHowtouseGlob()functiontofindfilesrecursivelyinPython?Python:PassingDictionaryasArgumentstoFunctionPython|PassingdictionaryaskeywordargumentsPythonExceptionHandlingPythonTryExceptErrorsandExceptionsinPythonBuilt-inExceptionsinPythonUser-definedExceptionsinPythonwithExamplesNZECerrorinPythonFileHandlinginPythonOpenaFileinPythonHowtoreadfromafileinPythonAddingnewcolumntoexistingDataFrameinPandasPythonmap()functionTakinginputinPythonHowtogetcolumnnamesinPandasdataframeReadJSONfileusingPythonTableofContentsPython|os.path.dirname()methodPython|os.path.join()methodOSPathmoduleinPythonOSModuleinPythonwithExamplesCreateadirectoryinPythonPython:CheckifaFileorDirectoryExistsPython|os.mkdir()methodPython|os.makedirs()methodPython|os.path.exists()methodPython|os.path.isfile()methodPython|os.path.isdir()methodPython|os.listdir()methodos.walk()inPythonHowtouseGlob()functiontofindfilesrecursivelyinPython?Python:PassingDictionaryasArgumentstoFunctionPython|PassingdictionaryaskeywordargumentsPythonExceptionHandlingPythonTryExceptErrorsandExceptionsinPythonBuilt-inExceptionsinPythonUser-definedExceptionsinPythonwithExamplesNZECerrorinPythonFileHandlinginPythonOpenaFileinPythonHowtoreadfromafileinPythonAddingnewcolumntoexistingDataFrameinPandasPythonmap()functionTakinginputinPythonHowtogetcolumnnamesinPandasdataframeReadJSONfileusingPythonImproveArticle SaveArticle LikeArticle Python|os.path.dirname()methodLastUpdated: 26Aug,2019OSmoduleinPythonprovidesfunctionsforinteractingwiththeoperatingsystem.OScomesunderPython’sstandardutilitymodules.Thismoduleprovidesaportablewayofusingoperatingsystemdependentfunctionality.os.pathmoduleissubmoduleofOSmoduleinPythonusedforcommonpathnamemanipulation.os.path.dirname()methodinPythonisusedtogetthedirectorynamefromthespecifiedpath.Syntax:os.path.dirname(path)Parameter:path:Apath-likeobjectrepresentingafilesystempath.ReturnType:Thismethodreturnsastringvaluewhichrepresentsthedirectorynamefromthespecifiedpath.Code:Useofos.path.dirname()method#Pythonprogramtoexplainos.path.dirname()method     #importingos.pathmodule importos.path  #Pathpath='/home/User/Documents'  #Getthedirectoryname  #fromthespecifiedpathdirname=os.path.dirname(path)  #Printthedirectoryname  print(dirname)    #Pathpath='/home/User/Documents/file.txt'  #Getthedirectoryname  #fromthespecifiedpathdirname=os.path.dirname(path)  #Printthedirectoryname  print(dirname)    #Pathpath='file.txt'  #Getthedirectoryname  #fromthespecifiedpathdirname=os.path.dirname(path)  #Printthedirectoryname  print(dirname)  #Intheabovespecifiedpath #doesnotcontainsany#directoryso, #ItwillprintNothing  Output: /home/User /home/User/Documents Reference:https://docs.python.org/3/library/os.path.htmlMyPersonalNotes arrow_drop_upSave LikeNext Python|os.path.join()methodRecommendedArticlesPage:11,May1728,Apr2014,Jul1613,Dec1603,May1709,May1721,Aug1921,Aug1921,Aug1921,Jun1921,Jun1903,Oct1812,Dec1804,Feb2010,Jul1903,Oct1812,Oct1716,Nov1717,Nov1720,Nov1720,Nov1723,Nov1713,Dec1706,Dec19ArticleContributedBy:ihritik@ihritikVotefordifficultyEasy Normal Medium Hard ExpertArticleTags:PythonOS-path-modulepython-os-modulePythonReportIssueWritingcodeincomment? Pleaseuseide.geeksforgeeks.org, generatelinkandsharethelinkhere. LoadCommentsWhat'sNewViewDetailsViewDetailsViewDetailsMostpopularinPythonPythonDictionaryReadafilelinebylineinPythonEnumerate()inPythonHowtoInstallPIPonWindows?IterateoveralistinPythonMorerelatedarticlesinPythonDifferentwaystocreatePandasDataframePythonString|replace()CreateaPandasDataFramefromListsPythonprogramtoconvertalisttostringReadingandWritingtotextfilesinPython× Weusecookiestoensureyouhavethebestbrowsingexperienceonourwebsite.Byusingoursite,you acknowledgethatyouhavereadandunderstoodour CookiePolicy& PrivacyPolicy GotIt! StartYourCodingJourneyNow!Login Register



請為這篇文章評分?