How to find current directory and file s directory

0 votes

In Python, what commands can I use to find:

  1. the current directory (where I was in the terminal when I ran the Python script), and
  2. where the file I am executing is?
Dec 17, 2020 in Python by Roshni
• 10,480 points
2,561 views

1 answer to this question.

0 votes

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)


To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants, and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")
answered Dec 17, 2020 by Gitika
• 65,770 points

Related Questions In Python

0 votes
1 answer

how to find files and skip directories in os.listdir

You need to filter out directories; os.listdir() ...READ MORE

answered Mar 6, 2019 in Python by SDeb
• 13,300 points
9,745 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,020 points
962 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 58,020 points
1,290 views
0 votes
0 answers

How to mix read() and write() on Python files in Windows

It appears that a write() immediately following a read() on a ...READ MORE

Oct 24, 2018 in Python by Aryya
• 500 points
815 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
3 answers

How to distinguish between a variable and an identifier?

An identifier actually identifies something, it's not the thing ...READ MORE

answered Dec 16, 2020 in Python by Rajiv
• 8,870 points
65,080 views
+1 vote
1 answer

How to get the home directory using ‘~’ in Python?

Hello, @Roshni, You need to import the os module, ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,770 points
863 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP