Python应用程序读取和写入当前工作目录作为.app / exe [英] Python app which reads and writes into its current working directory as a .app/exe
问题描述
我有一个python脚本,它在当前工作目录中读取一个名为data.txt的文本文件,然后将其中的数据转换为另一个单独的程序的json格式来处理。
我遇到的问题是,我不知道如何读取.txt文件(并写入一个新的文件),这是与。捆绑。我正在使用的当前方法不起作用,因为它是从终端运行,而不是作为一个.app执行的事实?
任何帮助感激!
Mac上的.app文件在启动时没有任何合理的当前工作目录。 p>
当然它有一些工作目录,你可以通过 好消息是,你显然不需要当前的工作目录;您需要.app包或.exe的目录。换句话说,如果有人这样做: \Users\foo> C:\Stuff\MyProgram.exe 您需要 在Windows上,这很容易。一个.exe就是一个文件,它的路径是你在Python中得到的 得到这个的正确方法是使用Cocoa(或者CoreFoundation): 不想这样做,你几乎不得不依靠一些文件说你不能依赖的信息,并且有一天可能会改变。但是下面的代码应该是安全的: 为了停止工作,脚本将不得不在.app包之外,或者在你正在寻找的另一个.app之内,否则捆绑包将不得不被命名为.app,或者他们会不得不停止正常的目录结构;没有一个在OS X 10中可能会发生变化。*,甚至是OS 11。 作为一个问题:你正在尝试做什么是最有可能的首先是一个坏主意。 Mac应用程序不应该与它一起使用文件。相反,如果用户希望在文件旁边工作,你可能需要一个简单的Unix可执行文件(或者只是一个普通的Python脚本,而不是一个应用程序)。 I have a python script which reads a text file in it's current working directory called "data.txt" then converts the data inside of it into a json format for another separate program to handle. The problem i'm having is that i'm not sure how to read the .txt file (and write a new one) which is in the same directory as the .app when the python script is all bundled up. The current method i'm using doesn't work because of something to do with it using the fact that it's ran from the terminal instead of executed as a .app? Any help is appreciated! A .app on the Mac doesn't have any reasonable current working directory when launched. Of course it has some working directory, and you can easily find out what it is at runtime by The good news is, you apparently don't actually want the current working directory; you need the directory of the .app bundle or .exe. In other words, if someone does this: You want On Windows, this is easy. An .exe is just a file, and its path will be the On Mac, it's harder. A .app is a bundle—a directory containing other files and directories. Somewhere in there is an executable interpreter and a copy of your script, and The correct way to get that is to use Cocoa (or CoreFoundation): If you don't want to do that, you pretty much have to rely on some information that the documentation says you can't rely on and could change some day. But the following code should be safe: In order for this to stop working, either the script would have to be outside the .app bundle, or inside another .app inside the one you're looking for, or bundles would have to stop being named .app, or they'd have to stop being structured as normal directories; none of this seems likely to change in OS X 10.*, or even OS Y 11. As a side issue: what you're trying to do is most likely a bad idea in the first place. A Mac application shouldn't be working with files alongside it. Conversely, if users are going to expect to work on files alongside it, you probably want a simple Unix executable (or just a plain Python script with chmod +x), not an application. 这篇关于Python应用程序读取和写入当前工作目录作为.app / exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! os.getcwd( )
,你可以测试在不同版本的OS X上启动的各种不同的方式来找出所有的模式,但是你有什么好处?
C:
C:\\ \\ Stuff
(可执行文件的目录),而不是 C:\Users\foo
(工作目录)。
__ path __
,所以:
import os
pathToApp = os.path.dirname(__ path__)
在Mac上,这很难。 .app是一个包含其他文件和目录的目录。在那里有一个可执行的解释器和你的脚本的副本, __ path __
会给你后者,而不是.app。的路径。 b
$ b
pre $ import Cocoa
pathToApp = Cocoa.NSBundle.mainBundle()。bundlePath()
import os
pathToApp = __file__
while not pathToApp.endswith ('.app'):
path = os.path.dirname(path)
os.getcwd()
, and you can test on a variety of different ways of launching on different versions of OS X to figure out all of the patterns, but what good does that do you?C:\Users\foo> C:\Stuff\MyProgram.exe
C:\Stuff
(the executable's directory), not C:\Users\foo
(the working directory).__path__
you get in Python, so:import os
pathToApp = os.path.dirname(__path__)
__path__
is going to give you the latter, not the path to the .app.import Cocoa
pathToApp = Cocoa.NSBundle.mainBundle().bundlePath()
import os
pathToApp = __file__
while not pathToApp.endswith('.app'):
path = os.path.dirname(path)