Python | os.path.realpath() method - GeeksforGeeks
文章推薦指數: 80 %
os.path.realpath() method in Python is used to get the canonical path of the specified filename by eliminating any symbolic links ... 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.realpath()methodPython|os.path.dirname()methodOSPathmoduleinPythonOSModuleinPythonwithExamplesPython|os.path.join()methodCreateadirectoryinPythonPython: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-definedExceptionsinPythonwithExamplesNZECerrorinPythonFileHandlinginPythonOpenaFileinPythonAddingnewcolumntoexistingDataFrameinPandasPythonmap()functionTakinginputinPythonHowtogetcolumnnamesinPandasdataframeReadJSONfileusingPythonTableofContentsPython|os.path.realpath()methodPython|os.path.dirname()methodOSPathmoduleinPythonOSModuleinPythonwithExamplesPython|os.path.join()methodCreateadirectoryinPythonPython: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-definedExceptionsinPythonwithExamplesNZECerrorinPythonFileHandlinginPythonOpenaFileinPythonAddingnewcolumntoexistingDataFrameinPandasPythonmap()functionTakinginputinPythonHowtogetcolumnnamesinPandasdataframeReadJSONfileusingPythonImproveArticle SaveArticle LikeArticle Python|os.path.realpath()methodLastUpdated: 18Jun,2019OSmoduleinPythonprovidesfunctionsforinteractingwiththeoperatingsystem.OScomesunderPython’sstandardutilitymodules.Thismoduleprovidesaportablewayofusingoperatingsystemdependentfunctionality.os.pathmoduleissubmoduleofOSmoduleinPythonusedforcommonpathnamemanipulation.os.path.realpath()methodinPythonisusedtogetthecanonicalpathofthespecifiedfilenamebyeliminatinganysymboliclinksencounteredinthepath.Syntax:os.path.realpath(path)Parameter:path:Apath-likeobjectrepresentingthefilesystempath.Apath-likeobjectiseitherastringorbytesobjectrepresentingapath.ReturnType:Thismethodreturnsastringvaluewhichrepresentsthecanonicalpath.CreateasoftlinkorsymboliclinkInUnixorLinux,softlinkorsymboliclinkcanbecreatedusinglncommand.Belowisthesyntaxtocreateasymboliclinkattheshellprompt: $ln-s{source-filename}{symbolic-filename} Example:Example:Inaboveoutput,“/home/ihritik/Desktop/file(shortcut).txt”isasymboliclink.Code:Useofos.path.realpath()methodtogetcanonicalpathandresolvesymboliclinks#Pythonprogramtoexplainos.path.realpath()method #importingosmodule importos #Pathpath="/home/ihritik/Desktop/file(shortcut).txt" #Getthecanonicalpath#ofthespecifiedpath#byeliminatinganysymboliclinks#encounteredinthepathreal_path=os.path.realpath(path) #Printthecanonicalpathprint(real_path) #Pathpath="/../../GeeksForGeeks/sample.py" #Getthecanonicalpath#ofthespecifiedpath#eliminatinganysymboliclinks#encounteredinthepathreal_path=os.path.realpath(path) #Printthecanonicalpathprint(real_path) #Pathpath="file.txt" #Getthecanonicalpath#ofthespecifiedpath#eliminatinganysymboliclinks#encounteredinthepathreal_path=os.path.realpath(path) #Printthecanonicalpathprint(real_path) os.chdir("/home/ihritik/Downloads/") #Pathpath="file.txt" #Getthecanonicalpath#ofthespecifiedpath#eliminatinganysymboliclinks#encounteredinthepathreal_path=os.path.realpath(path) #Printthecanonicalpathprint(real_path)Output: /home/ihritik/Documents/file(original).txt /GeeksForGeeks/sample.py /home/ihritik/file.txt /home/ihritik/Downloads/file.txt Reference:https://docs.python.org/3/library/os.path.htmlMyPersonalNotes arrow_drop_upSave LikeNext Python|os.path.dirname()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
延伸文章資訊
- 1How to Find Where Python is Installed on Windows - Data to Fish
- 2OS Path module in Python - Tutorialspoint
- 3__file__ (A Special variable) in Python - GeeksforGeeks
- 4os.getcwd()和os.path.realpath(__file__)的区别 - CSDN博客
1.os.path.realpath(__file__)是脚本所在的绝对路径,os.getcwd()是工作目录,默认情况下是一样的,但是把当前工作工作区修改后,输出就不一样了。
- 5os.path — Common pathname manipulations — Python 3.10 ...
The os.path module is always the path module suitable for the operating system Python is running ...