Python : How to Get the current working directory - thisPointer

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

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



請為這篇文章評分?