如何在python脚本目录中执行程序 [英] How to execute a program in python script directory

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

问题描述

我刚开始使用Python并尝试将一个简单的界面作为练习。现在我可以通过os.popen命令执行程序。但是,我必须指定程序所在的目录。例如,说:


os.popen(" c:\ program = files \programb.py")


是有什么方法我可以告诉Python假设默认目录是从哪里运行接口脚本?所以说我有一个文件夹,里面都是我的python文件。因此,当我单击按钮打开programb时,脚本知道只搜索该文件夹。我试图使所有内容都自包含,因此文件夹可能位于目录中的不同位置(可能是C:\,桌面,c:\program文件等)。顺便说一句,Python是一种令人敬畏的语言,我不敢相信我生命中没有进入过这种语言。

解决方案

既然你知道在原始脚本的路径中,您可以使用 os 模块从文件名中拆分目录,并将目录加入任何其他文件名。例如:

<跨度类= codeLink 的onclick =模糊(此,this.parentNode.parentNode,的getChildren(此),true);>展开 | 选择 | Wrap | 行号


非常感谢您回复bvdet!我想即使你误解了我,你仍然设法回答了我的一半问题!我要问的是我的主界面python文件和我要调用的任何其他python脚本都在同一个文件夹中。问题是文件夹可能并不总是在程序文件中;它可以在桌面上(可能是c:\documents和settings \ username \\\ todktop),它可能只是在c:\等等。所以目录可能会改变。有没有办法解析主界面文件位置,然后使用上面显示的拆分技术将它附加到我可能想从主界面文件调用的其他python脚本。


我之所以这么说是因为我要将所需的所有python脚本保存在一个文件夹中,我可能会迁移到另一个桌面环境,例如ubuntu,文件目录不会是相同,或者我可以将文件夹从我的C盘上的桌面移动到D盘等。抱歉误导;我也很难构思这个问题......



所以当我点击按钮打开programb时,脚本知道仅在该文件夹中搜索。我试图使所有内容都自包含,因此文件夹可能位于目录中的不同位置(可能是C:\,桌面,c:\ program文件等)



要运行的文件的路径必须是硬编码的,或者必须从用户或进程的运行时获取,无论是从哪里获取文件的工作方式,然后是可以很好地使用os.chdir(<在运行时>期间获得的PATH)并执行其他脚本或将PATH设置为变量并稍后访问它。


I am just starting out on Python and trying to make a simple interface as practice. Right now I can execute a program through the os.popen command. However, I have to specify the directory where the program is located. For example, say:

os.popen("c:\program^files\programb.py")

Is there any way I can tell Python to assume the default directory is wherever the interface script is ran from? So say I have a folder and inside are all my python files. So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.). By the way, Python is an AWESOME language, I can''t believe I didn''t get into this earlier in my life.

解决方案

Since you know the path to the original script, you can use the os module to split the directory from the file name, and join the directory to any other file name. Example:

Expand|Select|Wrap|Line Numbers


Thank you very much for the reply bvdet! I think even though you misunderstood me, you still managed to answer half of my question! What I am asking is my main interface python file and any other python scripts I want to call are all in the same folder. The issue is the folder may not always be in program files; it could be on desktop (which could be something like c:\documents and settings\username\desktop), it could be just in c:\, etc. So the directory may change. Is there any way to parse the main interface file location and then use the split technique you showed above to append it to my other python scripts that I might want to call from the main interface file.

The reason I''m asking this is since I''m going to keep all the python scripts I want in one folder, I might migrate to another desktop environment such as ubuntu, which the file directory wouldn''t be the same, or I could move the folder from desktop on my C drive to D drive, etc. Sorry for being misleading; it was difficult for me to frame the question too...


So when I click on a button to open programb, the script knows to only search in that folder. I am trying to make everything self-contained so the folder may be in different places in the directory (could be C:\, desktop, c:\program files, etc.)

The path for the file that is going to run has to be either hardcoded or must be obtained from the runtime from a user or process, either of the ways you can get where the file is working from, then you could very well use the os.chdir(<PATH obtained during runtime>) and execute the other scripts or set the PATH to a variable and access it later.


这篇关于如何在python脚本目录中执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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