查找脚本目录 [英] Finding Script Directory

查看:76
本文介绍了查找脚本目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的,跨平台的,找出目录的方式来运行

脚本?


我已经google了一点,但不能得到一个明确的答案。


在sys.argv [0]上,文档说:

argv [0]是脚本名称(取决于操作系统是否

这是一个完整的路径名)。


所以这似乎不是答案。


脚本目录在sys.path中总是*某处* - 但不是

总是在位置0.如果你使用py2exe那么sys.path [0 ]是进口的

zipfile !!


问候,


模糊

http://www.voidspace.org.uk/ atlantib ... thonutils.html

解决方案

Fuzzyman写道:

什么''是最好的,跨平台的,方式o f找出运行目录的目录?

我已经搜索了一下,但是得不到明确的答案。


我已经看到了二十个主题并且我仍然不知道/回想

是否有一个明确的答案。 :-(

在sys.argv [0]上,文档说:
argv [0]是脚本名称(取决于操作系统是否
这是一个完整的路径名或者没有)。

所以这似乎不是答案。


实际上,你只需要工作在Linux和Windows上,至少
,它只是脚本名称,如果你从当前目录运行

,或者路径(相对或绝对)到

运行脚本的目录。就像这样,无论是否支持
py2exe'。


__file__可能会有所帮助,但它不存在时

你有py2exe这个东西......

脚本目录在sys.path中总是* somewhere * - 但不总是在位置0。如果你使用py2exe,那么sys.path [0]是从@ !!导入的
zipfile / blockquote>


是的,sys.path对于这类事情并不是很好。

-Peter


os.curdir()有帮助吗?它至少告诉你

当前目录。我的解决方案是我的想法

你的问题是在我的配置文件中放入一个installdirectory = statement

,当

应用程序时会更新已安装。这样我总是知道安装脚本的位置是
。我使用Inno Installer

,在安装过程中更新配置文件非常容易




HTH,

Larry Bates

Syscon,Inc。


" Fuzzyman" < MI ***** @ foord.net>在消息中写道

news:80 ************************** @ posting.google.c om ...

找出运行目录的最佳,跨平台的方式是什么?

我已经google了一下,但是不能得到一个明确的答案。

在sys.argv [0]上,文档说:
argv [0]是脚本名称(无论是否依赖于操作系统) />这是一个完整的路径名。)

所以这似乎不是答案。

脚本目录总是在某个地方*在sys中。路径 - 但不是
总是在位置0.如果你使用py2exe那么sys.path [0]是它导入的
zip文件!!

问候, http ://www.voidspace.org.uk/atlantib...thonutils.html



Peter Hansen写道:
< blockquote class =post_quot es> Fuzzyman写道:

找出运行目录的最佳,跨平台的方式是什么?



我已经看到了20个线程,我仍然不知道/回忆
是否有明确的答案。 :-(

在sys.argv [0]上,文档说:
argv [0]是脚本名称(取决于操作系统是否
这个是一个完整的路径名。)

所以这似乎不是答案。



实际上,它就是你所有的'在Linux和Windows上,至少,它只有脚本名称,如果你从当前目录运行
,或者路径(相对或绝对)来自运行脚本的目录。无论是否也是这样,它都是这样的。

__ file__可以提供帮助,但是当你有py2exe这个东西时它不存在......

脚本目录在sys.path中总是*某处* - 但不是<总是在位置0.如果你使用py2exe,那么sys.path [0]是从导入的
zipfile !!



是啊,sys.path对我来说并不是很好这种事情。

-Peter




哦,来吧,彼得,你看到这个问题解决的次数比你

可以摇一摇。所需的代码是


import os.path,sys

print os.path.abspath(sys.argv [0])


记住这有多难?没有意识到你这么老了

;-) ...


问候

STeve


What''s the best, cross platform, way of finding out the directory a
script is run from ?

I''ve googled a bit, but can''t get a clear answer.

On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn''t seem to be the answer.

The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html

解决方案

Fuzzyman wrote:

What''s the best, cross platform, way of finding out the directory a
script is run from ?

I''ve googled a bit, but can''t get a clear answer.
I''ve seen twenty threads on this and I still don''t know/recall
whether there''s a clear answer. :-(
On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn''t seem to be the answer.
Actually, it''s all you''ve got to work with. On Linux and Windows,
at least, it''s got either just the script name, if you''re running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It''s like this whether
py2exe''d or not, too.

__file__ could be of assistance, but it doesn''t exist when
you''ve py2exe the thing...
The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!



Yeah, sys.path isn''t much good for this sort of thing.

-Peter


Would os.curdir() help? It as least tells you the
current directory. My solution to what I think is
your problem is to put an installdirectory= statement
in my configuration file that gets updated when the
application is installed. That way I always know
where the script is installed. I use Inno Installer
and it is very easy to update configuration files
during installation.

HTH,
Larry Bates
Syscon, Inc.

"Fuzzyman" <mi*****@foord.net> wrote in message
news:80**************************@posting.google.c om...

What''s the best, cross platform, way of finding out the directory a
script is run from ?

I''ve googled a bit, but can''t get a clear answer.

On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn''t seem to be the answer.

The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html



Peter Hansen wrote:

Fuzzyman wrote:

What''s the best, cross platform, way of finding out the directory a
script is run from ?

I''ve googled a bit, but can''t get a clear answer.


I''ve seen twenty threads on this and I still don''t know/recall
whether there''s a clear answer. :-(

On sys.argv[0] the docs say :
argv[0] is the script name (it is operating system dependent whether
this is a full pathname or not).

So this doesn''t seem to be the answer.


Actually, it''s all you''ve got to work with. On Linux and Windows,
at least, it''s got either just the script name, if you''re running
from the current directory, or a path (relative or absolute) to
the directory where the script is run from. It''s like this whether
py2exe''d or not, too.

__file__ could be of assistance, but it doesn''t exist when
you''ve py2exe the thing...

The script directory is always *somewhere* in sys.path - but not
always in position 0. If you use py2exe then sys.path[0] is the
zipfile it does the imports from !!


Yeah, sys.path isn''t much good for this sort of thing.

-Peter



Oh, come now, Peter, you''ve seen this problem solved more times than you
can shake a stick at. The required code is

import os.path, sys
print os.path.abspath(sys.argv[0])

How hard can that be to remember? Didn''t realise you were getting so OLD
;-) ...

regards
STeve


这篇关于查找脚本目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆