ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

how to write data into a file inside the subscriber callback function?

asked 2021-08-16 11:59:23 -0500

Mattew_underscore gravatar image

updated 2021-08-16 13:29:34 -0500

Hi everyone, I am pretty new in learning Python and ROS and I have a little problem. I am publishing an input function into a topic and a subscriber reads the needed data and computes the convolution in real-time to get the response of the linear system of the matter. this callback function works at 50 Hz. I wrote this into the callback function in order to save a specific response value for each time step.

f = open("response.txt", "a")
f.write("\n")
f.close()

the problem is pretty trivial. it does not create any file in that directory and in any other as well. could you give me a feedback about this? Thank you in advance

edit retag flag offensive close merge delete

Comments

Thomas D gravatar image Thomas D  ( 2021-08-16 22:06:24 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-08-21 07:00:52 -0500

Mike Scheutzow gravatar image

roslaunch defaults to making the current directory $HOME/.ros, but you shouldn't count on that. There are multiple methods to override this behavior. Instead, do as #q11642 suggests and always use a full path name. You can read the HOME environment variable to avoid hard-coding your own login in the code. In python2:

fname = os.getenv('HOME') + '/response.txt'

A completely separate issue is that it is grossly inefficient to open and close a file 50 times per second. I would make f a global variable and open it one time when you start, and close it one time when done.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2021-08-16 11:59:23 -0500

Seen: 170 times

Last updated: Aug 21 '21