本文翻译自:How can I find script's directory with Python? [duplicate]
This question already has an answer here: 这个问题在这里已有答案:
- How to properly determine current script directory? 如何正确确定当前脚本目录? 13 answers 13个答案
Consider the following Python code: 请考虑以下Python代码:
import os
print os.getcwd()
I use os.getcwd()
to get the script file's directory location . 我使用os.getcwd()
来获取脚本文件的目录位置 。 When I run the script from the command line it gives me the correct path whereas when I run it from a script run by code in a Django view it prints /
. 当我从命令行运行脚本时,它给了我正确的路径,而当我从Django视图中的代码运行的脚本运行它时,它打印/
。
How can I get the path to the script from within a script run by a Django view? 如何从Django视图运行的脚本中获取脚本的路径?
UPDATE: 更新:
Summing up the answers thus far - os.getcwd()
and os.path.abspath()
both give the current working directory which may or may not be the directory where the script resides. 总结到目前为止的答案 - os.getcwd()
和os.path.abspath()
都给出了当前的工作目录,该目录可能是也可能不是脚本所在的目录。 In my web host setup
gives only the filename without the path. 在我的web主机设置中, __file__
只提供没有路径的文件名。 __file__
Isn't there any way in Python to (always) be able to receive the path in which the script resides? 在Python中没有任何方法(总是)能够接收脚本所在的路径吗?
#1楼
参考:https://stackoom.com/question/Khle/如何使用Python查找脚本的目录-重复
#2楼
This code: 这段代码:
import os
dn = os.path.dirname(os.path.realpath(__file__))
sets "dn" to the name of the directory containing the currently executing script. 将“dn”设置为包含当前正在执行的脚本的目录的名称。 This code: 这段代码:
fn = os.path.join(dn,"vcb.init")
fp = open(fn,"r")
sets "fn" to "script_dir/vcb.init" (in a platform independent manner) and opens that file for reading by the currently executing script. 将“fn”设置为“script_dir / vcb.init”(以独立于平台的方式)并打开该文件以供当前正在执行的脚本读取。
Note that "the currently executing script" is somewhat ambiguous. 请注意,“当前正在执行的脚本”有些模棱两可。 If your whole program consists of 1 script, then that's the currently executing script and the "sys.path[0]" solution works fine. 如果您的整个程序包含1个脚本,那么这是当前正在执行的脚本,并且“sys.path [0]”解决方案正常工作。 But if your app consists of script A, which imports some package "P" and then calls script "B", then "PB" is currently executing. 但是如果你的应用程序包含脚本A,它导入一些包“P”然后调用脚本“B”,那么“PB”当前正在执行。 If you need to get the directory containing "PB", you want the " os.path.realpath(__file__)
" solution. 如果需要获取包含“PB”的目录,则需要“ os.path.realpath(__file__)
”解决方案。
" __file__
" just gives the name of the currently executing (top-of-stack) script: "x.py". “ __file__
”只是给出了当前正在执行的(栈顶)脚本的名称:“x.py”。 It doesn't give any path info. 它不提供任何路径信息。 It's the "os.path.realpath" call that does the real work. 这是执行实际工作的“os.path.realpath”调用。
#3楼
This is a pretty old thread but I've been having this problem when trying to save files into the current directory the script is in when running a python script from a cron job. 这是一个非常古老的线程,但是当我尝试将文件保存到脚本所在的当前目录中时,我遇到了这个问题,从cron作业运行python脚本。 getcwd() and a lot of the other path come up with your home directory. getcwd()和你的主目录提供了很多其他路径。
to get an absolute path to the script i used 获得我使用的脚本的绝对路径
directory = os.path.abspath(os.path.dirname(__file__))
#4楼
Here's what I ended up with. 这就是我最终的结果。 This works for me if I import my script in the interpreter, and also if I execute it as a script: 如果我在解释器中导入脚本,并且如果我将其作为脚本执行,这对我有用:
import os
import sys
# Returns the directory the current script (or interpreter) is running in
def get_script_directory():
path = os.path.realpath(sys.argv[0])
if os.path.isdir(path):
return path
else:
return os.path.dirname(path)
#5楼
Try this: 试试这个:
def get_script_path(for_file = None):
path = os.path.dirname(os.path.realpath(sys.argv[0] or 'something'))
return path if not for_file else os.path.join(path, for_file)
#6楼
使用os.path.abspath('')