{"id":19543,"date":"2022-05-19T00:24:13","date_gmt":"2022-05-18T18:54:13","guid":{"rendered":"https:\/\/java2blog.com\/?p=19543"},"modified":"2022-05-19T00:24:13","modified_gmt":"2022-05-18T18:54:13","slug":"print-current-directory-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/print-current-directory-python\/","title":{"rendered":"Print Current Directory in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Files_and_Directories_in_Python\">Files and Directories in Python<\/a><\/li><li><a href=\"#Ways_to_print_the_Current_Directory_in_Python\">Ways to print the Current Directory in Python<\/a><ul><li><a href=\"#Using_the_osgetcwd_function_to_print_the_current_directory_in_Python\">Using the os.getcwd() function to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_ospath_module_to_print_the_current_directory_in_Python\">Using the os.path module to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_ospathabspath_function_to_print_the_current_directory_in_Python\">Using the os.path.abspath() function to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_pathlibPathcwd_function_to_print_the_current_directory_in_Python\">Using the pathlib.Path.cwd() function to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_pathlibPathresolve_function_to_print_the_current_directory_in_Python\">Using the pathlib.Path.resolve() function to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_pathlibPathparent_attribute_to_print_the_current_directory_in_Python\">Using the pathlib.Path.parent attribute to print the current directory in Python<\/a><\/li><li><a href=\"#Using_the_locatethis_dir_function_to_print_the_current_directory_in_Python\">Using the locate.this_dir() function to print the current directory in Python<\/a><\/li><\/ul><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<p>In this post, we will see how to print current directory in Python.<\/p>\n<h2><span id=\"Files_and_Directories_in_Python\">Files and Directories in Python<\/span><\/h2>\n<p>A computer system uses directories to store and organize files. Every file is present in some directory. There is a root folder at the top of the hierarchy, and every directory is a child of this root.<\/p>\n<p>In Python, we have the current directory of a script file. We also have the current directory of the Python terminal. At times, both are the same especially when we work with Python notebook files.<\/p>\n<h2><span id=\"Ways_to_print_the_Current_Directory_in_Python\">Ways to print the Current Directory in Python<\/span><\/h2>\n<p>We will now discuss how to print the current directory in Python. We will deal with methods and display the directory of the running Python script or the current directory of the Python terminal.<\/p>\n<h3><span id=\"Using_the_osgetcwd_function_to_print_the_current_directory_in_Python\">Using the <code>os.getcwd()<\/code> function to print the current directory in Python<\/span><\/h3>\n<p>We have different functionalities in the <code>os<\/code> module that can communicate and interact with the operating system.<\/p>\n<p>The current working directory is the directory in which the Python terminal is operating. At times, it may not be the directory of the Python script.<\/p>\n<p>We can return the current working directory using the <code>os.getcwd()<\/code> function. It returns the directory as a string.<\/p>\n<p>For example, <\/p>\n<pre code = \"python\" title=\"Using os.getcwd()\">\nimport os\nprint(os.getcwd())    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<h3><span id=\"Using_the_ospath_module_to_print_the_current_directory_in_Python\">Using the <code>os.path<\/code> module to print the current directory in Python<\/span><\/h3>\n<p>The <code>os.path<\/code> module provides different pathname-related functions. We can use them to print the current directory in Python.<\/p>\n<p>The <code>__file__<\/code> constant will be used in these methods and it represents the pathname of the Python script file. This path is relative to the current working directory. Remember, to use this constant we need to work in a <code>.py<\/code> file and not on a notebook file. <\/p>\n<p>The <code>os.path.realpath()<\/code> function from this module can return the path of a given file. It removes any symbolic links present in the path. We can pass the <code>__file__<\/code> constant to this function and we will get the path of the file we are working with. We can display this path to print the current directory in Python.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title=\"Using os.path.realpath\">\nimport os\nprint(os.path.realpath(__file__))    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\\file.py\n<\/div>\n<p>In the above example, we can observe that this method prints the full path and the filename. <\/p>\n<p>We can use the <code>os.path.dirname()<\/code> function to only print the current directory in Python without the filename. This function accepts a path with a filename and returns the directory.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport os\nprint(os.path.dirname(os.path.realpath(__file__)))    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<p>We can also split the result of the <code>os.path.realpath()<\/code> function. For this, we will use the <code>os.path.split()<\/code> function. It splits a given path into two parts. The second part contains the last component of the path and the first part contains everything up to the second part. We can use it to split the path and print the current directory in Python.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport os\npath, fname = os.path.split(full_path)\nprint(path)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/get-parent-directory-python\/\" target=\"_blank\" rel=\"noopener\">Get Parent Directory in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/get-parent-directory-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-list-files-directory\/\" target=\"_blank\" rel=\"noopener\">Python list files in directory<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-list-files-directory\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h3><span id=\"Using_the_ospathabspath_function_to_print_the_current_directory_in_Python\">Using the <code>os.path.abspath()<\/code> function to print the current directory in Python<\/span><\/h3>\n<p>This method may not work in all situations so should be used as a last resort. It returns the full pathname of the current file with the specified path. We can provide an empty string (think of it as an empty path), and it will return the path of the current file.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\">\nimport os\nprint(os.path.abspath(''))    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<h3><span id=\"Using_the_pathlibPathcwd_function_to_print_the_current_directory_in_Python\">Using the <code>pathlib.Path.cwd()<\/code> function to print the current directory in Python<\/span><\/h3>\n<p>Python 3.4 introduces a new efficient library to work with paths and filenames. The <code>pathlib<\/code> library was introduced to create and work with <code>Path<\/code> objects that can adapt to different operating systems.<\/p>\n<p>The <code>pathlib.Path<\/code> class of this library has a variety of functions to work on path-related operations. We can use it to print the current directory in Python. The <code>pathlib.Path.cwd()<\/code> function can be used to return the current working directory in a <code>pathlib<\/code> object.  <\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nfrom pathlib import Path\nprint(Path.cwd())    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<p>Remember, this method returns the current working directory not the directory of the running file.<\/p>\n<h3><span id=\"Using_the_pathlibPathresolve_function_to_print_the_current_directory_in_Python\">Using the <code>pathlib.Path.resolve()<\/code> function to print the current directory in Python<\/span><\/h3>\n<p>As discussed, we can work with pathnames with the <code>pathlib.Path<\/code> objects. We can specify the <code>__file__<\/code> constant in the constructor of this class to create a <code>pathlib<\/code> object.<\/p>\n<p>To get the full path with the filename, we can use the <code>resolve()<\/code> function. This method will return the full path and remove any symbolic links that occur in the path.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\">\nfrom pathlib import Path\nf_path = Path(__file__)\np = f_path.resolve()\nprint(p)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\\file1.py\n<\/div>\n<h3><span id=\"Using_the_pathlibPathparent_attribute_to_print_the_current_directory_in_Python\">Using the <code>pathlib.Path.parent<\/code> attribute to print the current directory in Python<\/span><\/h3>\n<p>The <code>pathlib.Path.parent<\/code> library returns the parent of the given path. We can use it to remove the filename from the previous method and only print the current directory in Python.<\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\">\nfrom pathlib import Path\nf_path = Path(__file__)\np = f_path.resolve().parent\nprint(p)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<h3><span id=\"Using_the_locatethis_dir_function_to_print_the_current_directory_in_Python\">Using the <code>locate.this_dir()<\/code> function to print the current directory in Python<\/span><\/h3>\n<p>The <code>locate<\/code> module can also help to print the current directory in Python. We can use the <code>this_dir()<\/code> function from this library to return the directory of the current Python script.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\">\nimport locate\nprint(locate.this_dir())    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nC:\\Users\\Username\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>This tutorial demonstrated how to print the current directory in Python. To wrap up, we worked with three modules. These were the <code>os<\/code>, <code>pathlib<\/code>, and <code>locate<\/code> modules. <\/p>\n<p>The <code>getcwd()<\/code> function of the <code>os<\/code> module returns the current working directory and its submodule <code>os.path<\/code> has different functions to return the current directory of the file. This module works with Python 2 as well. <\/p>\n<p>We also discussed the <code>pathlib<\/code> library, which works with Python 3.4 and above. These methods were more efficient as they return <code>pathlib<\/code> objects with the current directory. The <code>locate.this_dir()<\/code> function also worked to print the current directory in Python.  <\/p>\n<p>THat&#8217;s all about how to print current directory in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsFiles and Directories in PythonWays to print the Current Directory in PythonUsing the os.getcwd() function to print the current directory in PythonUsing the os.path module to print the current directory in PythonUsing the os.path.abspath() function to print the current directory in PythonUsing the pathlib.Path.cwd() function to print the current directory in PythonUsing the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[185,145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19543"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=19543"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/19543\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=19543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=19543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=19543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}