简体   繁体   English

返回文件的文件路径,而不是当前目录

[英]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.

相关问题 如何将目录或制作目录添加到当前路径并使用文件 - How to add directory or make directory to current path and use file 无法打开文件命名目录路径+文件名+当前日期和时间 - cannot open file named directory path + filename + current date and time python unipath:当前文件目录(祖先)的路径未输出任何内容 - python unipath: path to current file directory (ancestor) outputs nothing 如何获取当前文件目录的完整路径? - How do I get the full path of the current file's directory? 为什么 PyCharm 将当前文件和父目录添加到路径? - Why PyCharm adds current file and parent directory to path? Python os模块在当前目录上打开文件,具有相对路径 - Python os module open file above current directory with relative path 将当前文件目录添加到 PyCharm 中的 Python 解释器路径 - Add current file directory to Python interpreter path in PyCharm 有没有办法获取不在当前工作目录中的文件的绝对路径(在 Python 中)? - Is there a way to get an absolute path of a file which is not in the current working directory (in Python)? 如何在 Windows 批处理/Python 文件中获取当前目录的路径? - How to get path of current directory in Windows Batch/Python file? 这个 function 应该返回给定文件或目录的路径,但什么都不做 - this function should return a path to a given file or directory, but instead does nothing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM