python __file__ 与argv[0]

python __file__ 与argv[0]

在python下,获取当前执行主脚本的方法有两个:sys.argv[0]和__file__。

sys.argv[0]

获取主执行文件路径的最佳方法是用sys.argv[0],他就是脚本的第一个参数,所以它的值在本模块以及被调用模块中都保持不变,它可能是一个相对路径,输入的是绝对路径就是绝对路径,输入的是相对路径就是相对路径。所以再取一下abspath是保险的做法,像这样:

import os,sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
print "running from", dirname
print "file is", filename

__file__

__file__ 是用来获得模块所在的路径的,这可能得到的是一个相对路径,比如在脚本test.py中写入:

#!/usr/bin/env python
print __file__

  • 按相对路径./test.py来执行,则打印得到的是相对路径,
  • 按绝对路径执行则得到的是绝对路径。
  • 而按用户目录来执行(~/practice/test.py),则得到的也是绝对路径(~被展开)
  • 所以为了得到绝对路径,我们需要 os.path.realpath(__file__)。
  • 在模块中调用子模块,子模块中输出的也是绝对路径

而在Python控制台下,直接使用print __file__是会导致  name ‘__file__’ is not defined错误的,因为这时没有在任何一个脚本下执行,自然没有 __file__的定义了。

os.getcwd()函数是获得当前的工作目录,与该模块在哪个目录无关。即显示pwd的内容。

__file__和argv[0]差异

在主执行文件中时,两者没什么差异,不过要是在不同的文件下,就不同了,下面示例:

Z:\mycode\a.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
print "show_where: realpath is",os.path.realpath(__file__)
from c import b
b.show_where()
 
Z:\mycode\c\b.py
import sys, os
def show_where():
    print "show_where: sys.argv[0] is", repr(sys.argv[0])
    print "show_where: __file__ is", repr(__file__)
    print "show_where: cwd is", repr(os.getcwd())
    print "show_where: realpath is",os.path.realpath(__file__)
 
-bash-4.1$ python a.py
script: sys.argv[0] is 'a.py'
script: __file__ is 'a.py'
script: cwd is '/dbc/pek2-dbc201/danyangw/mycode'
script: realpath is /dbc/pek2-dbc201/danyangw/mycode/a.py
show_where: sys.argv[0] is 'a.py'
show_where: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/c/b.pyc'
show_where: cwd is '/dbc/pek2-dbc201/danyangw/mycode'
show_where: realpath is /dbc/pek2-dbc201/danyangw/mycode/c/b.pyc


若绝对路径执行则:

-bash-4.1$ python /dbc/pek2-dbc201/danyangw/mycode/a.py
script: sys.argv[0] is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
script: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
script: cwd is '/dbc/pek2-dbc201/danyangw'
script: realpath is /dbc/pek2-dbc201/danyangw/mycode/a.py
show_where: sys.argv[0] is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
show_where: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/c/b.py'
show_where: cwd is '/dbc/pek2-dbc201/danyangw'
show_where: realpath is /dbc/pek2-dbc201/danyangw/mycode/c/b.py



所以一般来说,argv[0]要更可靠些。

### ROS 中 `executable_file` 的定义及用途 在 ROS(Robot Operating System)环境中,`executable_file` 通常指的是可执行文件,这些文件可以由多种方式生成并用于运行特定的功能模块。以下是关于其定义、类型、作用以及如何生成的相关说明。 #### 定义类型 在 ROS 中,`executable_file` 可以是由 C++ 或 Python 编写的程序编译后的二进制文件或脚本文件。它们通常是通过构建工具链(如 Catkin 或 Colcon)生成的。具体来说: - **C++ 执行文件**:当开发者编写了一个基于 ROS 节点的 C++ 程序后,可以通过 `colcon build` 或 `catkin_make` 将源代码编译成二进制可执行文件[^1]。 - **Python 脚本文件**:对于 Python 开发者而言,可以直接创建 `.py` 文件作为节点入口,并将其标记为可执行文件以便于启动[^3]。 #### 主要作用 `executable_file` 在 ROS 生态中的主要功能包括但不限于以下几个方面: 1. 启动独立的 ROS 节点,负责完成某项具体的任务,比如传感器数据采集、控制算法实现等; 2. 提供服务端或者客户端逻辑支持,使得不同组件之间能够相互请求和服务响应; 3. 实现动作服务器/客户机模式下的长时间复杂操作管理; 4. 加载模型实体到仿真环境当中去,例如利用 Gazebo 插件加载 sdf 和 urdf 格式的机器人描述文件; #### 如何生成 Executable File? 为了生成一个可用的 executable,在开发过程中需要遵循以下流程之一: - 对于 C++ 来说,先按照标准模板写好 cpp 源码之后配置 package.xml 添加依赖关系再修改 cmakelists.txt 设置目标名称链接库等等最后执行 colcon build 构建项目即可得到最终产物位于 devel/lib/<package_name>/ 下面找到对应名字即为目标exe; - 如果采用 python 则比较简单只需要确保 script 具备 shebang 行(`#!/usr/bin/env python`) 并赋予 execute permission chmod +x filename.py 即可通过 rosrun 命令直接调用. ```bash chmod +x your_script.py rosrun your_package your_script.py ``` --- ### 示例代码展示 下面给出一段简单的例子演示如何创建一个最小化的 ROS Node 使用 c++: ```cpp #include "rclcpp/rclcpp.hpp" class MinimalPublisher : public rclcpp::Node { public: MinimalPublisher() : Node("minimal_publisher") { publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10); timer_ = this->create_wall_timer( std::chrono::seconds(1), std::bind(&MinimalPublisher::timer_callback, this)); } private: void timer_callback(){ auto message = std_msgs::msg::String(); message.data = "Hello World"; RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); publisher_->publish(message); } rclcpp::TimerBase::SharedPtr timer_; rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_; }; int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<MinimalPublisher>()); rclcpp::shutdown(); return 0; } ``` 上述示例展示了怎样建立基本发布者节点的过程。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值