in Education by
In Python What command can I use to find these: Location of file which I am executing, and Current directory (where I was in the terminal at the time I ran the script in Python) Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
To get the path to the directory where the Python file is stored write the code below in that file: import os directory_path = os.path.dirname(os.path.realpath(__file__)) The above code may fail if you have already used os.chdir() to change the working of your current directory because the value of _file_ constant is relative with the current working directory and does not change by an os.chdir() call. If you want to get the current working directory use the following code: import os cw_d = os.getcwd() Here, os.getcwd() returns a string representing the current working directory. Hope it helps!

Related questions

0 votes
    How can I check the file going to be written exists in the directory, and if not , How can I create directory ... " flag for the same? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the ... based on the keys? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In the os module in Python, is there a way to find if a directory exists, something like: os.direxists(os ... in pseudocode True/False Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Can someone tell me how to find all the files in directory with .txt extension in Python? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How do I get a list of all files (and directories) in a given directory in Python? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am facing problems in _files which isn't defined if I execute the script with exec, ececfile and in _module_ ... help me with this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I delete a file or a folder in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I import the functions from file.py to another Python file which is in: app/app1/Folder1/xyz. ... folder1.xyz import func_name Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to get current time in Python, what are the method/module to do so? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    There are two codes, what are the difference between them: Using type import types if type(q) is types. ... ): do_something_else() Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me the basic difference between @classmethod and @staticmmethod, I am good with C++ so you ... we use them? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    What * and ** do for param2 in the following: def foo(param1, *param2): def bar(param1, **param2): Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...