Python Program to Get the Full Path of the Current Working ...
文章推薦指數: 80 %
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
延伸文章資訊
- 1Get the path of running file (.py) in Python - nkmk note
Use os.path.basename() , os.path.dirname() to get the file name and the directory name of the run...
- 2How to find the absolute and relative path of a file in Python
Call os.path.abspath(path) with path as the file name to get the absolute pathname. ... To create...
- 3How to get an absolute file path in Python - Stack Overflow
Given a path such as "mydir/myfile.txt" , how do I find the file's absolute path relative to the ...
- 4在Python 中獲取絕對路徑
要使用 pathlib 獲得絕對路徑,需要從 pathlib 模組中匯入 Path 類,並使用該類的 Path.absolute() 函式來確定給定檔案或資料夾的絕對路徑。 Python.
- 5Find path to the given file using Python - GeeksforGeeks
We can get the location (path) of the running script file .py with __file__. __file__ is useful f...