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)
Related
My directory structure like below:
./outputsetting.json
./myapp/app.py
I load outputsetting.json file in app.py like below:
with open("..\outputpath.json","r") as f:
j=json.load(f)
And run in myapp directory it's ok:
python app.py
But if I run app.py in the parent directory:
python .\myapp\app.py
It raise error
FileNotFoundError: [Errno 2] No such file or directory: '..\\outputpath.json'
How can I load file from disk by the relative directory to the app.py? So I can run it from any place and needn't modify code every time.
Thanks!
When you start the script from the parent directory, the working directory for the script changes. If you want to access a file that has a specific location relative to the script itself, you can do this:
from pathlib import Path
location = Path(__file__).parent
with open(location / "../outputsetting.json","r") as f:
j=json.load(f)
Path(__file__) gets the location of the script it is executed in. The .parent thus gives you the folder the script is in, still as a Path object. And you can then navigate to the parent directory from that with location / "../etc"
Or of course in one go:
with open(Path(__file__).parent / "../outputsetting.json","r") as f:
j=json.load(f)
(Note: your code says outputpath.json, but I assume that's the same outputsetting.json)
Another solution would be to just change the working directory to be the folder the script is in - but that of course only works if all your scripts and modules are OK with that change:
from pathlib import Path
from os import chdir
chdir(Path(__file__).parent)
with open("../outputsetting.json","r") as f:
j=json.load(f)
I'd prefer constructing the absolute path as in the previous example though.
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.
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().
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
How do I find the directory of the running Python script from inside Python [3.3]?
I have tried what was suggested at: How can I find script's directory? , but I get "Invalid syntax" and directed to "os" (And I did import os).
The closest I have got to the answer is: sys.argv[0], but that still includes the file name, so I cannot use it. Is there any other way?
The part where it says rundir = sys.argv[0] is where the suggested code will go:
import os, sys
rundir = sys.argv[0]
print("Running from" + rundir)
To get the directory that contains the module you are running:
import os
path = os.path.dirname(os.path.realpath(__file__))
Or if you want the directory from which the script was invoked:
import os
path = os.getcwd()
From the docs:
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file.
Depending on how the script is called, this may be a relative path from os.getcwd(), so os.path.realpath(__file__) will convert this to an absolute path (or do nothing is the __file__ is already an absolute path). os.path.dirname() will then return the full directory by stripping off the filename.
Try this:
import os
os.path.dirname(__file__)
__file__ gets the name of the file you are in. The dirname function gets the directory that the file is in.
The syntax error probably had to do with the print statement. In python 3.x
print "hi"
is invalid. Print is now a function.
print("hi")
works. You need the parentheses.
This should work:
import os,sys
print(os.path.dirname(os.path.realpath(sys.argv[0])))