简体   繁体   English

如何找到运行python脚本的.app目录

[英]How to find the directory of the .app that a python script is running from

I recently made a python script that goes through files in whatever directory it is placed in and renames them based on certain criteria. 我最近制作了一个python脚本,该脚本可以遍历文件所在目录中的文件,并根据某些条件对其进行重命名。 The script works perfectly, and I compiled the script into an OS X .app using py2app. 该脚本运行完美,我使用py2app将脚本编译为OS X .app。 This worked fine as well. 这也很好。 However now when I run the script, it searches through the files in the ".app/contents/macOS" folder (where the script is located) rather than where the ".app" is actually located. 但是,现在运行脚本时,它会搜索“ .app / contents / macOS”文件夹(脚本所在的位置)中的文件,而不是“ .app”的实际位置。

This is because it has this code at the start: 这是因为它的开头有以下代码:

src = os.path.dirname(os.path.abspath(__file__))

which assigns the the location of the ".py" file to a variable which is then used extensively throughout the script. 它将“ .py”文件的位置分配给一个变量,该变量随后在整个脚本中得到广泛使用。 Is there any way I can instead add a snippet of code which tells python the path location of the ".app" that the ".py" file is executing from? 有什么方法可以代替我添加一段代码,告诉python执行“ .py”文件的“ .app”的路径位置吗?

If not, perhaps there is a way to get a file explorer window open, from there it would be possible for a user to select a folder who's path would then get assigned to the "src" variable. 如果没有,也许有一种方法可以打开文件浏览器窗口,用户可以从中选择一个文件夹,然后将该文件夹的路径分配给“ src”变量。 I'm very new to python however so this would certainly be a challenge. 我对python非常陌生,因此这无疑是一个挑战。

Try to use: 尝试使用:

import os
print(os.getcwd())

From docs: 从文档:

getcwd() getcwd()

Return a unicode string representing the current working directory. 返回表示当前工作目录的unicode字符串。

To find the location of the .app directory that wraps the application, the most direct way is to modify the path that you find with your current code. 若要查找包装应用程序的.app目录的位置,最直接的方法是用当前代码修改找到的路径。 After computing src as you did, just trim it like this and use app as the path: 在像您一样计算src之后,只需像这样修剪它并使用app作为路径:

import re
...
app, rest = re.split(r"/[^/]*\.app/", src, 1)

This stops at the first path component that ends in .app . 这在以.app结尾的第一个路径组件处停止。 If you prefer you can hard-code your application name, eg /myprogram.app/ . 如果愿意,可以对应用程序名称进行硬编码,例如/myprogram.app/

PS. PS。 I'm puzzled as to why you copy your app to the folder you want to modify. 为什么将您的应用程序复制到要修改的文件夹,我感到困惑。 A file selection dialog, or drag and drop, is the more common (and easier) way to tell a program what to work on. 文件选择对话框(或拖放)是告诉程序要执行的操作的更常见(更简单)的方法。 OS X applications created with py2app support drag and drop: Your program can get the path to the dropped directory like this: 使用py2app创建的OS X应用程序支持拖放操作:您的程序可以获取到放置目录的路径,如下所示:

import sys
folders = sys.argv[1:]   # Handles multiple arguments
for folder in folders:
    do_something_in(folder)

Then simply drag and drop the directory or directories you want to process onto the application icon, which you can just keep on your desktop, the Finder's sidebar, or a favorite directory -- no need to copy it each time you use it, and no need for it to know where its source is. 然后只需将要处理的一个或多个目录拖放到应用程序图标上即可,您可以将其保留在桌面,Finder的侧边栏或收藏夹目录中-每次使用时都无需复制它,并且无需需要它知道其来源在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM