os.getcwd() can't find current working directory - python

Whenever I try to get current working directory in python on windows with os.getcwd() it shows only the root directory.
for example this directory structure:
root\
ㄴbase\
ㄴㄴfile\
ㄴㄴㄴfile.py\
I use
# files.py
os.getcwd()
output will be:
\---> c:/root\
I want to get c:/root/file
any idea?
thanks in advance
+
actually os.getcwd() finds the 'root:\base\' at some point. but in other cases it doesn't find. so I wanted to know the difference.

Current working directory isn't the path of the script, it's where you are running the script from.
And apparently, you're running your script from c:/root/ directory. If you want the directory of the script use this to get the script path
from os.path import dirname, abspath
script_path = abspath(dirname(__file__))
or run the script from c:/root/base/file to get the path with getcwd().

Related

python change default directory

so I recently tried using playsound and the .mp3 is in the same folder as the .py, and it says it could not find the file.
I printed out the current directory and it says it's at C:\Users\me
Shouldn't this be at the same directory as where the .py script is at? This has been happening with my other python scripts where I have to explicitly give it the directory, whereas before I didn't have to and it was just where the python script was at.
Is there a setting for this?
The current directory is defined by where you execute the python file not the location of it. If you want to change the directory you can use the os module:
import os
os.chdir(PATH)
If you're looking for a file in the same directory as the executing python program:
basename = os.path.basename(__file__)
my_file_name = basename + "/myfile"
with open(my_file_name) as my_file:
...
The variable __file__ contains the full pathname of the currently executing Python program.

OS .CWD Skips One DIR how to fix

I am trying to get directory from where script is ran, but os.cwd() skips one dir
Script Path - D:\Mainfolder\Testfolder\testfoldr2\check1.py
But
Output is - D:\Mainfolder\Testfolder
Why no testfolder2 is coming though the script is stored there kindly suggest how to get testfolder2 as working dir
I am Using for checking
import os
print(os.getcwd())
getcwd() will give you your working directory from where you are running the script,
if you want folder of the file then you can use below code:
import os
path = os.path.dirname(__file__)
print(path)

Creating directory on relative path in Python and running in absolute path

I have the following python script (script.py):
#!/usr/bin/env python3
import os
os.makedirs('./downloads/')
Which simply creates a directory named 'downloads' in the directory, where the script.py file is located.
Now I want this program to be run as a cronjob in linux. So the command for that is:
./home/pi/application/script.py
The folder, the program creates should be created under '/home/pi/application/' but it is created in the root directory '/'
How can I fix this?
To get the path where script.py is rather than the path from where you call the script (working directory) you can use:
os.path.dirname(os.path.abspath(__file__))
In you example, the previous command would return /home/pi/application/ (if that is the full path).

Get path of file being run after cd to another directory in python

I have following simple script test.py in directory /Users/apps:-
import os
os.chdir("/Users/apps/update/diiferent_path")
path = os.path.dirname(os.path.abspath(__file__))
print(path)
I am getting path value as /Users/apps/update/diiferent_path because I have changed the directory. How can I get test.py path i.e. /Users/apps after changing directory.
It's impossible to do it after the chdir (unless, of course, if __file__ already contains the absolute path), because the information is lost. There is no such thing as what was the current directory when the process started stored anywhere automatically. But you can store it manually by calling os.path.abspath(__file__) before calling the chdir.
I agree with pts post that storing the path is the way to go. It best shows your intention of getting the original path, despite what modifications are done throughout the script.
For reference sake two workarounds. The path changes because the script is executed by name. The interpreter then finds the file in the current directory. The current directory changes, and thus the absolute path of both __file__ and sys.argv.
One way around that is to call the script with an absolute path. Another is to realize that the path of the script is automatically added to the PYTHONPATH, i.e. to sys.path. The following script illustrates that:
#!/usr/bin/python
import os
import sys
os.chdir("/usr/bin")
print(os.path.abspath(__file__))
print(os.path.abspath(sys.argv[0]))
print(sys.path[0])
The output:
# /tmp$ ./so24323731.py
/usr/bin/so24323731.py
/usr/bin/so24323731.py
/tmp
# /tmp$ /tmp/so24323731.py
/tmp/so24323731.py
/tmp/so24323731.py
/tmp

Is it possible to specify the previous directory python?

I am attempting to make a Python testing script module self-contained directory-wise for scalability and maintainability purposes.
I have done some research and haven't been able to find the answer i'm looking for. Basically, I would like to determine if there is a function in Python that can do something similar to cd - in shell.
I would generally like to avoid typing in full path-names, and have all the paths relative to a specified environment.
Example:
I have my main directory where my scripts are located python-scripts, I have folders for each testing environment demo, and a screenshot folder for each testing environment demo-screenshots.
If i wanted to save a screenshot to the screenshot with Python, while working in the demo directory, I would just send it to the directory below: demo-screenshots, using the path '/demo-screenshots/example.png'. The problem is that I don't understand how to move back a directory.
Thank you.
You're looking to change the working directory? The OS module in python has a lot of functions to help with this.
import os
os.chdir( path )
path being ".." to go up one directory. If you need to check where you are before/after a change of directory you can issue the getcwd() command:
mycwd = os.getcwd()
os.chdir("..")
#do stuff in parent directory
os.chdir(mycwd) # go back where you came from
path = os.path.dirname(__file__)
print(path)
will print the CWD of the file, say C:\Users\Test\Documents\CodeRevamp\Joke
path2 = os.path.dirname(path)
print(path2)
will print the Parent directory of of the file: C:\Users\Test\Documents\CodeRevamp

Categories