[英]Return the file path of the file, not the current directory
I have a Python/Sikuli script where I need to store the location of the file in a variable.我有一个 Python/Sikuli 脚本,我需要将文件的位置存储在一个变量中。
I have the IDE on one location, and the file is stored in a different directory.我的 IDE 位于一个位置,文件存储在不同的目录中。
IDE: C:\\Sikuli\\runIDE.cmd IDE: C:\\Sikuli\\runIDE.cmd
File: C:\\Script\\Files\\HelloWorld2.sikuli\\HelloWorld.py文件:C:\\Script\\Files\\HelloWorld2.sikuli\\HelloWorld.py
Now I use os.getcwd()
to fetch the location of the IDE.现在我使用
os.getcwd()
来获取 IDE 的位置。
But how do I return the full directory path of the file "HelloWorld.py"?但是如何返回文件“HelloWorld.py”的完整目录路径?
I also used os.path.abspath("HelloWorld2.py")
and os.path.dirname(os.path.realpath('HelloWorld2.py'))
.我还使用了
os.path.abspath("HelloWorld2.py")
和os.path.dirname(os.path.realpath('HelloWorld2.py'))
。
But also that didn't work.但这也不起作用。
Can anyone help me with this?谁能帮我这个?
In regular Python, using __file__
returns the full path of the directory that contains the executed file:在常规 Python 中,使用
__file__
返回包含执行文件的目录的完整路径:
os.path.dirname( os.path.abspath( __file__ ) )
However, in Sikuli, the __file__
reference is undefined:但是,在 Sikuli 中,
__file__
引用是未定义的:
from sikuli import *
import os
print( os.path.dirname( os.path.abspath( __file__ ) ) )
When run in Sikuli v2.0.4, we see that __file__
is unsupported :在 Sikuli v2.0.4 中运行时,我们看到
__file__
不受支持:
[error] script [ test2 ] stopped with error in line 6
[error] NameError ( name '__file__' is not defined )
[error] --- Traceback --- error source first
line: module ( function ) statement
6: main ( <module> ) print( os.path.dirname( os.path.abspath(__file__) ) )
[error] --- Traceback --- end --------------
Instead, use getBundle()
, such as:相反,使用
getBundle()
,例如:
from sikuli import *
import os
print( os.path.dirname( getBundlePath() ) )
Sikuli seems to have some issue's with the __file__
Sikuli 似乎对
__file__
有一些问题
What does work with Sikuli and Python is: Sikuli 和 Python 的作用是:
os.path.dirname(getBundlePath())
Edit for the question below:编辑以下问题:
To get the names of the files that are in the directory you can do:要获取目录中文件的名称,您可以执行以下操作:
path = os.path.dirname(getBundlePath())
directory = os.listdir(path)
for file in directory:
print(file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.