0

I want to create a python ros2 node that reads some data from a csv file and then use it, this csv file is put in the dirctory :workspace/src/<pkg_name>/<pkg_name> beside the ros2 python node

when i run this node from this directory it works fine , but when i build the pkg using colcon build and run this node using ros2 run <pkg_name> <node_name> it produce an error no such file or directory

the solution is putting the full path when I open the csv

but my question here how can not use the full path and still make it work, where does my node get copied when I use colcon build work, or what ros2 specific internal variables that i may use

    #with open("/home/ali/Engineering/ITI/low_speed_self_driving_vehicles/ROS_lectures/GWS/src/iti_lab_9/iti_lab_9/turtle_commands.csv",'r', newline='') as csvfile:
    with open("turtle_commands.csv",'r', newline='') as csvfile:
        csvread = csv.reader(csvfile, delimiter=',')
        next(csvread)
        for row in csvread:
            self.linear_x.append(float(row[0]))
            self.angular_z.append(float(row[1]))
    self.index=0
Ali Hamdy
  • 63
  • 4

1 Answers1

2

Because when you launch the package the launch path is very probably not next to the csv.

You could add this to your script

import os

def path_script():
    _path = os.path.dirname(os.path.abspath(__file__))
    _path = _path.replace('\\', '/')
    return _path

path_script()

to check that.

user3732793
  • 1,699
  • 4
  • 24
  • 53