Python : How to Get the current working directory - thisPointer
文章推薦指數: 80 %
It returns a string containing the current working directory. For calling this function we need to import os module i.e.. import ... Skiptocontent InthisarticlewewilldiscusshowtogetthecurrentworkingdirectoryinPython. Python’sosmoduleprovidesafunctiontogetthecurrentworkingdirectoryi.e. os.getcwd() Itreturnsastringcontainingthecurrentworkingdirectory. Forcallingthisfunctionweneedtoimportosmodulei.e. importos Let’sunderstandbyanexample, GettheCurrentWorkingDirectoryinPython ''' GetCurrentworkingDirectory ''' currentDirectory=os.getcwd() Itwillreturnthecurrentworkingdirectory.Inoursystemoutputwas, /home/varun/python/tutorials Let’schangethecurrentworkingdirectoryto”. ''' ChangetheCurrentworkingDirectory ''' os.chdir('/home/varun') Nowlet’sgetthecurrentworkingdirectory, currentDirectory=os.getcwd() Nowoutputinoursystemwas, /home/varun becausewechangedthecurrentworkingdirectory. Advertisements Completeexampleisasfollows, importos defmain(): ''' GetCurrentworkingDirectory ''' currentDirectory=os.getcwd() print(currentDirectory) ''' ChangetheCurrentworkingDirectory ''' os.chdir('/home/varun') ''' GetCurrentworkingDirectory ''' currentDirectory=os.getcwd() print(currentDirectory) if__name__=='__main__': main() Output: /home/varun/python/tutorials /home/varun PandasTutorials-LearnDataAnalysiswithPython PandasTutorialPart#1-IntroductiontoDataAnalysiswithPython PandasTutorialPart#2-BasicsofPandasSeries PandasTutorialPart#3-Get&SetSeriesvalues PandasTutorialPart#4-Attributes&methodsofPandasSeries PandasTutorialPart#5-AddorRemovePandasSerieselements PandasTutorialPart#6-IntroductiontoDataFrame PandasTutorialPart#7-DataFrame.loc[]-SelectRows/ColumnsbyIndexing PandasTutorialPart#8-DataFrame.iloc[]-SelectRows/ColumnsbyLabelNames PandasTutorialPart#9-FilterDataFrameRows PandasTutorialPart#10-Add/RemoveDataFrameRows&Columns PandasTutorialPart#11-DataFrameattributes&methods PandasTutorialPart#12-HandlingMissingDataorNaNvalues PandasTutorialPart#13-IterateoverRows&ColumnsofDataFrame PandasTutorialPart#14-SortingDataFramebyRowsorColumns PandasTutorialPart#15-MergingorConcatenatingDataFrames PandasTutorialPart#16-DataFrameGroupByexplainedwithexamples AreyoulookingtomakeacareerinDataSciencewithPython? DataScienceisthefuture,andthefutureisherenow.DataScientistsarenowthemostsought-afterprofessionalstoday.TobecomeagoodDataScientistortomakeacareerswitchinDataScienceonemustpossesstherightskillset.WehavecuratedalistofBestProfessionalCertificateinDataSciencewithPython.ThesecourseswillteachyoutheprogrammingtoolsforDataSciencelikePandas,NumPy,Matplotlib,SeabornandhowtousetheselibrariestoimplementMachinelearningmodels. CheckouttheDetailedReviewofBestProfessionalCertificateinDataSciencewithPython. Remember,DataSciencerequiresalotofpatience,persistence,andpractice.So,startlearningtoday. JoinaLinkedInCommunityofPythonDevelopers Postnavigation ←PreviousPostNextPost→ RelatedPosts 1thoughton“Python:HowtoGetthecurrentworkingdirectory?” Whataboutcurrentdirectoryofthefile?ifitsinasubfolder Reply LeaveaCommentCancelReplyYouremailaddresswillnotbepublished.Requiredfieldsaremarked*Typehere..Name* Email* Website Δ ThissiteusesAkismettoreducespam.Learnhowyourcommentdataisprocessed. Advertisements Advertisements RecentPosts RemoverowswithNaNvaluesfromNumpyArray–Python RemoveelementsfromNumPyArraybyIndex AddavaluetoeachelementofanarrayinPython ReplaceLastCharacterofaStringinC++ ReplaceFirstCharacterinastringinC++ ReplacealloccurrencesofacharacterinStringinC++ Replaceallcharactersinstringwithasterisks–Python ReplaceLastoccurrenceofaStringinPython ReplacewordsinastringusingdictionaryinPython ReplacefirstoccurrenceofasubstringinPython PythonTutorialsLookingforSomething Searchfor: Search Wevalueyourprivacy Toprovidethebestexperiences,weusetechnologieslikecookiestostoreand/oraccessdeviceinformation.ConsentingtothesetechnologieswillallowustoprocessdatasuchasbrowsingbehaviororuniqueIDsonthissite.Notconsentingorwithdrawingconsent,mayadverselyaffectcertainfeaturesandfunctions. Functional Functional Alwaysactive Thetechnicalstorageoraccessisstrictlynecessaryforthelegitimatepurposeofenablingtheuseofaspecificserviceexplicitlyrequestedbythesubscriberoruser,orforthesolepurposeofcarryingoutthetransmissionofacommunicationoveranelectroniccommunicationsnetwork. Preferences Preferences Thetechnicalstorageoraccessisnecessaryforthelegitimatepurposeofstoringpreferencesthatarenotrequestedbythesubscriberoruser. Statistics Statistics Thetechnicalstorageoraccessthatisusedexclusivelyforstatisticalpurposes. Thetechnicalstorageoraccessthatisusedexclusivelyforanonymousstatisticalpurposes.Withoutasubpoena,voluntarycomplianceonthepartofyourInternetServiceProvider,oradditionalrecordsfromathirdparty,informationstoredorretrievedforthispurposealonecannotusuallybeusedtoidentifyyou. Marketing Marketing Thetechnicalstorageoraccessisrequiredtocreateuserprofilestosendadvertising,ortotracktheuseronawebsiteoracrossseveralwebsitesforsimilarmarketingpurposes. Manageoptions Manageservices Managevendors Readmoreaboutthesepurposes Accept Deny Viewpreferences Savepreferences Viewpreferences {title} {title} {title} Wevalueyourprivacy Toprovidethebestexperiences,weusetechnologieslikecookiestostoreand/oraccessdeviceinformation.ConsentingtothesetechnologieswillallowustoprocessdatasuchasbrowsingbehaviororuniqueIDsonthissite.Notconsentingorwithdrawingconsent,mayadverselyaffectcertainfeaturesandfunctions. Functional Functional Alwaysactive Thetechnicalstorageoraccessisstrictlynecessaryforthelegitimatepurposeofenablingtheuseofaspecificserviceexplicitlyrequestedbythesubscriberoruser,orforthesolepurposeofcarryingoutthetransmissionofacommunicationoveranelectroniccommunicationsnetwork. Preferences Preferences Thetechnicalstorageoraccessisnecessaryforthelegitimatepurposeofstoringpreferencesthatarenotrequestedbythesubscriberoruser. Statistics Statistics Thetechnicalstorageoraccessthatisusedexclusivelyforstatisticalpurposes. Thetechnicalstorageoraccessthatisusedexclusivelyforanonymousstatisticalpurposes.Withoutasubpoena,voluntarycomplianceonthepartofyourInternetServiceProvider,oradditionalrecordsfromathirdparty,informationstoredorretrievedforthispurposealonecannotusuallybeusedtoidentifyyou. Marketing Marketing Thetechnicalstorageoraccessisrequiredtocreateuserprofilestosendadvertising,ortotracktheuseronawebsiteoracrossseveralwebsitesforsimilarmarketingpurposes. Manageoptions Manageservices Managevendors Readmoreaboutthesepurposes Accept Deny Viewpreferences Savepreferences Viewpreferences {title} {title} {title} Manageconsent Manageconsent ScrolltoTop
延伸文章資訊
- 1How to get current directory in Python? - ItsMyCode
To get current directory in Python, use the os module function os.getcwd(), and if you want to ch...
- 2Python : How to Get the current working directory - thisPointer
It returns a string containing the current working directory. For calling this function we need t...
- 3os.path — Common pathname manipulations — Python 3.10 ...
The os.path module is always the path module suitable for the operating system Python is running ...
- 4How to change current working directory in python - thisPointer
First of all we need to import python's os module i.e. ... Python's os module provides a function...
- 5Change current working directory with Python - GeeksforGeeks
To change the current working directory(CWD) os.chdir() method is used. This method changes the C...