Python Program to Get the Full Path of the Current Working ...

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

Example 1: Using pathlib module · Pass the file's name in Path() method. · parent gives the logical parent of the path and absolute() gives the absolute path of ... CourseIndex ExploreProgramiz Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnPythonpractically andGetCertified. ENROLL PopularTutorials GettingStartedWithPython PythonifStatement whileLoopinPython PythonLists DictionariesinPython StartLearningPython PopularExamples Addtwonumbers Checkprimenumber Findthefactorialofanumber PrinttheFibonaccisequence Checkleapyear ExplorePythonExamples ReferenceMaterials Built-inFunctions ListMethods DictionaryMethods StringMethods Viewall LearningPaths Challenges LearnPythonInteractively TryforFree Courses BecomeaPythonMaster BecomeaCMaster ViewallCourses Python JavaScript SQL C C++ Java Kotlin Swift C# DSA LearnPythonpractically andGetCertified. ENROLLFORFREE! PopularTutorials GettingStartedWithPython PythonifStatement whileLoopinPython PythonLists DictionariesinPython StartLearningPython AllPythonTutorials ReferenceMaterials Built-inFunctions ListMethods DictionaryMethods StringMethods Viewall Python JavaScript C C++ Java Kotlin LearnPythonpractically andGetCertified. ENROLLFORFREE! PopularExamples Addtwonumbers Checkprimenumber Findthefactorialofanumber PrinttheFibonaccisequence Checkleapyear AllPythonExamples PythonExamples MergeMails FindtheSize(Resolution)ofaImage FindHashofFile SafelyCreateaNestedDirectory CatchMultipleExceptionsinOneLine CopyaFile PythonProgramReadaFileLinebyLineIntoaList AppendtoaFile RelatedTopics PythonDirectoryandFilesManagement PythonPackage PythonModules Pythonopen() PythonInput,OutputandImport PythonGetCurrenttime PythonProgramtoGettheFullPathoftheCurrentWorkingDirectory Inthisexample,youwilllearntogetthefullpathofthecurrentworkingdirectory. Tounderstandthisexample,youshouldhavetheknowledgeofthefollowingPythonprogrammingtopics:PythonFileI/OPythonDirectoryandFilesManagement Example1:Usingpathlibmodule importpathlib #pathofthegivenfile print(pathlib.Path("my_file.txt").parent.absolute()) #currentworkingdirectory print(pathlib.Path().absolute()) Output /Users/username /Users/username Usingthepathlibmodule,youcangetthecurrentworkingdirectory. Passthefile'snameinPath()method. parentgivesthelogicalparentofthepathandabsolute()givestheabsolutepathofthefile. pathlib.Path().absolute()givesthecurrentworkingdirectory. Example2:Usingosmodule importos #pathofthegivenfile print(os.path.dirname(os.path.abspath("my_file.txt"))) #currentworkingdirectory print(os.path.abspath(os.getcwd())) Output /Users/username /Users/username Youcandothesamethingwiththeosmodule. Useabspath()methodtogetanabsolutepath. getcwd()givesthecurrentworkingdirectory. Shareon: Didyoufindthisarticlehelpful? Sorryaboutthat. Howcanweimproveit? Feedback* Leavethisfieldblank RelatedExamplesPythonExampleSafelyCreateaNestedDirectoryPythonExampleGettheFileNameFromtheFilePathPythonTutorialPythonDirectoryandFilesManagementPythonExampleExtractExtensionFromtheFileName GetApp GetPythonApp



請為這篇文章評分?