Generic path to read a file in a ROS directory
I have created a /csv
and /scripts
directories in my /src
directory. The /csv
directory contains a csv file and /scripts
directory contains a Python script to parse the csv file. The first few lines of the script is as follows -
import csv
with open('/home//default_ws/src/<package_name>/csv/alarms.csv', 'rb') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
How can I add a generic location path for the csv file in my Python script? And what modifications do I need to make in my CMakelists.txt
in order to install the /csv
and /scripts
directories?
* CORRECT ANSWER *
import csv
import os, rospkg
rospack = rospkg.RosPack()
with open(os.path.join(rospack.get_path("package_name"), "csv", "alarms.csv"), 'rb') as csvfile:
reader = csv.reader(csvfile)
for row in reader: