Return文件的文件路径,不是当前目录
Return the file path of the file, not the current directory
我有一个 Python/Sikuli 脚本,我需要在其中将文件的位置存储在一个变量中。
我有一个位置的 IDE,文件存储在另一个目录中。
IDE: C:\Sikuli\runIDE.cmd
文件:C:\Script\Files\HelloWorld2.sikuli\HelloWorld.py
现在我使用 os.getcwd()
获取 IDE 的位置。
但是我如何 return 文件 "HelloWorld.py" 的完整目录路径?
我还用了os.path.abspath("HelloWorld2.py")
和os.path.dirname(os.path.realpath('HelloWorld2.py'))
。
但这也没有用。
谁能帮我这个?
在常规 Python 中,使用 __file__
returns 包含已执行文件的目录的完整路径:
os.path.dirname( os.path.abspath( __file__ ) )
但是,在 Sikuli 中,__file__
引用未定义:
from sikuli import *
import os
print( os.path.dirname( os.path.abspath( __file__ ) ) )
Sikuli v2.0.4中运行时,我们看到__file__
是unsupported:
[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 --------------
而是使用getBundle()
,例如:
from sikuli import *
import os
print( os.path.dirname( getBundlePath() ) )
Sikuli 似乎对 __file__
有一些问题
与 Sikuli 和 Python 一起工作的是:
os.path.dirname(getBundlePath())
编辑以下问题:
要获取目录中文件的名称,您可以执行以下操作:
path = os.path.dirname(getBundlePath())
directory = os.listdir(path)
for file in directory:
print(file)
我有一个 Python/Sikuli 脚本,我需要在其中将文件的位置存储在一个变量中。
我有一个位置的 IDE,文件存储在另一个目录中。
IDE: C:\Sikuli\runIDE.cmd
文件:C:\Script\Files\HelloWorld2.sikuli\HelloWorld.py
现在我使用 os.getcwd()
获取 IDE 的位置。
但是我如何 return 文件 "HelloWorld.py" 的完整目录路径?
我还用了os.path.abspath("HelloWorld2.py")
和os.path.dirname(os.path.realpath('HelloWorld2.py'))
。
但这也没有用。
谁能帮我这个?
在常规 Python 中,使用 __file__
returns 包含已执行文件的目录的完整路径:
os.path.dirname( os.path.abspath( __file__ ) )
但是,在 Sikuli 中,__file__
引用未定义:
from sikuli import *
import os
print( os.path.dirname( os.path.abspath( __file__ ) ) )
Sikuli v2.0.4中运行时,我们看到__file__
是unsupported:
[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 --------------
而是使用getBundle()
,例如:
from sikuli import *
import os
print( os.path.dirname( getBundlePath() ) )
Sikuli 似乎对 __file__
有一些问题
与 Sikuli 和 Python 一起工作的是:
os.path.dirname(getBundlePath())
编辑以下问题:
要获取目录中文件的名称,您可以执行以下操作:
path = os.path.dirname(getBundlePath())
directory = os.listdir(path)
for file in directory:
print(file)