How to read files on Raspberry Pi

I want to know how to use fill in node in Raspberry Pi.

On Windows, I need to use escape path separators, for example: \Users\myUser.
But in Raspberry Pi I don't know how to fill in the address of the document.

Below is the json code of my process:

[
    {
        "id": "99d8cb51.cf4ce8",
        "type": "tab",
        "label": "test fill",
        "disabled": false,
        "info": ""
    },
    {
        "id": "348e443f.2578ac",
        "type": "inject",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 380,
        "wires": [
            [
                "eb7198f5.d48468"
            ]
        ]
    },
    {
        "id": "21ee1722.c20778",
        "type": "debug",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 360,
        "wires": []
    },
    {
        "id": "eb7198f5.d48468",
        "type": "function",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "func": "var tmp = new Date(msg.payload);\nmsg.payload = tmp\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 320,
        "y": 380,
        "wires": [
            [
                "21ee1722.c20778"
            ]
        ]
    },
    {
        "id": "cc9b4c3d.eb30c",
        "type": "inject",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 130,
        "y": 140,
        "wires": [
            [
                "bd1f76a0.ec28e8"
            ]
        ]
    },
    {
        "id": "bd1f76a0.ec28e8",
        "type": "function",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "func": "msg.filename = \"\\\\/:home\\\\pi\\\\1.txt\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 290,
        "y": 180,
        "wires": [
            [
                "a941e8b1.478778"
            ]
        ]
    },
    {
        "id": "e09e72a5.45955",
        "type": "debug",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 650,
        "y": 120,
        "wires": []
    },
    {
        "id": "a941e8b1.478778",
        "type": "file in",
        "z": "99d8cb51.cf4ce8",
        "name": "",
        "filename": "",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "x": 460,
        "y": 200,
        "wires": [
            [
                "e09e72a5.45955"
            ]
        ]
    }
]

Hi @Ian
Up the top of the edit window (see picture) there is an icon like: </>

You press that then paste the code.

Screenshot from 2020-09-03 15-14-50

Makes it easier for people to get access to it.

Mind trying again?

:joy:

[{"id":"d5830d92.707c1","type":"tab","label":"test rpi","disabled":false,"info":""},{"id":"62596014.67924","type":"inject","z":"d5830d92.707c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":320,"wires":[["5f100b5c.cb1d64"]]},{"id":"4b11cf91.fd6","type":"debug","z":"d5830d92.707c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":320,"wires":[]},{"id":"f49e35c1.2260f8","type":"file in","z":"d5830d92.707c1","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":610,"y":320,"wires":[["4b11cf91.fd6"]]},{"id":"5f100b5c.cb1d64","type":"function","z":"d5830d92.707c1","name":"","func":"msg.filename=\"\\\\home\\\\pi\\\\1.txt\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":320,"wires":[["f49e35c1.2260f8"]]},{"id":"2dfdfc6c.a56f14","type":"function","z":"d5830d92.707c1","name":"function","func":"// initialise the counter to 0 if it doesn't exist already\nvar dcount = global.get('dcount')||0;\ndcount += 1;\n \n// store the value back\nglobal.set('dcount',dcount);\n\nvar __dirname = \"\\\\home\\\\pi\";\n\nmsg.path = __dirname;\nmsg.dirctory = \"newDirctory_\" + dcount;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"// 节点正在停止或重新部署时,将运行此处添加的代码。 \ncount = 0;","x":540,"y":520,"wires":[["45e61d3c.c23824"]]},{"id":"a55cbf07.29c8c","type":"inject","z":"d5830d92.707c1","name":"inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":350,"y":520,"wires":[["2dfdfc6c.a56f14"]]},{"id":"45e61d3c.c23824","type":"fs-ops-mkdir","z":"d5830d92.707c1","name":"mkdir","path":"path","pathType":"msg","dirname":"dirctory","dirnameType":"msg","mode":"777","fullpath":"directory","fullpathType":"msg","x":770,"y":520,"wires":[[]]}]`Preformatted text`


I do't know how to fill in the address of the document.

Ah. Ok.

Oh, that post. Still not good. There is some stuff that (somehow) got stuck on the end.

Something about Preformatted text
(How did you export the code)

But to your question:

Node-red is sandboxed in how it works.

I'm sure there are ways to break out of the sandbox, but for the sake of explaining I am not going to go there.

It is in the settings.js file which is in the .node-red directory.

You will need to read stuff on how to set that up for file access.

I'm not even going to try and explain it. I'm still learning myself.

image
no such file or directory, open 'file'"

Try this

thank you,I tried this code and it works fine.

[{"id":"d5830d92.707c1","type":"tab","label":"test rpi","disabled":false,"info":""},{"id":"62596014.67924","type":"inject","z":"d5830d92.707c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":320,"wires":[["5f100b5c.cb1d64"]]},{"id":"4b11cf91.fd6","type":"debug","z":"d5830d92.707c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":320,"wires":[]},{"id":"f49e35c1.2260f8","type":"file in","z":"d5830d92.707c1","name":"","filename":"file","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":610,"y":320,"wires":[["4b11cf91.fd6"]]},{"id":"5f100b5c.cb1d64","type":"function","z":"d5830d92.707c1","name":"","func":"msg.filename=\"\\\\home\\\\pi\\\\1.txt\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":320,"wires":[["f49e35c1.2260f8"]]},{"id":"2dfdfc6c.a56f14","type":"function","z":"d5830d92.707c1","name":"function","func":"// initialise the counter to 0 if it doesn't exist already\nvar dcount = global.get('dcount')||0;\ndcount += 1;\n \n// store the value back\nglobal.set('dcount',dcount);\n\nvar __dirname = \"\\\\home\\\\pi\";\n\nmsg.path = __dirname;\nmsg.dirctory = \"newDirctory_\" + dcount;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"// 节点正在停止或重新部署时,将运行此处添加的代码。 \ncount = 0;","x":540,"y":520,"wires":[["45e61d3c.c23824"]]},{"id":"a55cbf07.29c8c","type":"inject","z":"d5830d92.707c1","name":"inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":350,"y":520,"wires":[["2dfdfc6c.a56f14"]]},{"id":"45e61d3c.c23824","type":"fs-ops-mkdir","z":"d5830d92.707c1","name":"mkdir","path":"path","pathType":"msg","dirname":"dirctory","dirnameType":"msg","mode":"777","fullpath":"directory","fullpathType":"msg","x":770,"y":520,"wires":[[]]}]

Good. So what is the problem again?

The problem is the same as at the beginning, the file cannot be read. Because it is running nodered on Raspberry Pi, it is different from windows. I don't know how to fill in the access address.

Maybe it’s wrong to fill in like this

I don't know that is allowed.

Have you read the link I sent?

That is all about sharing and accessing files.

I don't think the double \ are helping either.

Normally I think it should be:

"\home\pi\1.txt"

As said: I am a learner. read the link and I hope that will help you.

I am on my phone so can't look at the flow at the moment, but in case it helps you can use forward slashes on Windows, and I think you must use forward slashes on Linux, so use forward everywhere and the escape problem is no more.

1 Like

Ah, yes. Silly me.

"/home/pi/1.txt"

See how silly I am.

Show me a bad example and I am confused.

The problem is ok now.
First need to create a new file under the docker

docker exec -it mynodered bash
mkdir home/pi
touch 1.txt
vi 1.txt ## to add something like "hello"

because the nodered running under the docker of Raspberry Pi.
so it just can read file under the docker.

./home/pi/1.txt
now it is ok.

[{"id":"d5830d92.707c1","type":"tab","label":"test rpi","disabled":false,"info":""},{"id":"62596014.67924","type":"inject","z":"d5830d92.707c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":290,"y":320,"wires":[["f49e35c1.2260f8"]]},{"id":"4b11cf91.fd6","type":"debug","z":"d5830d92.707c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":320,"wires":[]},{"id":"f49e35c1.2260f8","type":"file in","z":"d5830d92.707c1","name":"","filename":"./home/pi/1.txt","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":540,"y":320,"wires":[["4b11cf91.fd6"]]},{"id":"5f100b5c.cb1d64","type":"function","z":"d5830d92.707c1","name":"","func":"msg.filename=\"\\/root\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":450,"y":160,"wires":[[]]},{"id":"2dfdfc6c.a56f14","type":"function","z":"d5830d92.707c1","name":"function","func":"// initialise the counter to 0 if it doesn't exist already\nvar dcount = global.get('dcount')||0;\ndcount += 1;\n \n// store the value back\nglobal.set('dcount',dcount);\n\nvar __dirname = \"\\\\/\";\n\nmsg.path = __dirname;\nmsg.dirctory = \"newDirctory_\" + dcount;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"// 节点正在停止或重新部署时,将运行此处添加的代码。 \ncount = 0;","x":540,"y":520,"wires":[["45e61d3c.c23824"]]},{"id":"a55cbf07.29c8c","type":"inject","z":"d5830d92.707c1","name":"inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":350,"y":520,"wires":[["2dfdfc6c.a56f14"]]},{"id":"45e61d3c.c23824","type":"fs-ops-mkdir","z":"d5830d92.707c1","name":"mkdir","path":"path","pathType":"msg","dirname":"dirctory","dirnameType":"msg","mode":"777","fullpath":"directory","fullpathType":"msg","x":770,"y":520,"wires":[[]]}]

image

Where is this home/pi in the scheme of things?

Because (given this is a RasPi) there should be (already) a directory home/pi

But you are talking docker so I can't really say.

Yes,in RasPi already a directory home/pi
but,there is no such directory in docker.
because the nodered running under the docker of Raspberry Pi.
I created a directory under nodered of docker.home/pi
Running under Docker

Your answer("/home/pi/1.txt") is very helpful to me,thank you!

mkdir home/pi has some problem
should use

mkdir home
cd home
mkdir pi

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.