Python file read is not working

The following code gives “FileNotFoundError: [Errno 44] No such file or directory: ‘/tmp/test.txt’” error. What am I doing wrong?

import os
import pandas as pd

os.system(‘pwd’)
os.system(‘sh “/Users/pcannata/Mine/My Repositories/AI/n8n/test.sh” > “/tmp/test.txt”’)
#df = pd.read_csv(“/tmp/test.txt”, delimiter=‘\t’)
with open(‘/tmp/test.txt’, “r”) as test:
print(test.read())

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
  • n8n version: 1.64.3
  • Running n8n via (Docker, npm, n8n cloud, desktop app): desltop app
  • Operating system: Mac OS M4

Hi @PhilC,

I’m assuming that you’re running this in the code node? The error you’re seeing suggests that Python couldn’t find the file /tmp/test.txt, which means either the file didn’t exist or there was an issue with the path or permissions.

Could you check that you have the correct path and permissions to the file? I also noticed that you’re using smart quotes (‘’ “”) instead of the standard straight quotes (’ "), could you replace them with the standard quotes and try again?

If you’re still unsure what’s causing the issue, could you share your workflow, not just the python code, and what you’re trying to do with it? Would help us reproduce the issue :slight_smile:

Tip for sharing your workflow in the forum

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

Make sure that you’ve removed any sensitive information from your workflow and include dummy data or pinned data as much as you can!


Hope that helps!

Hello @aya If you put the following code in a python node

import os

os.system(‘echo Hello > /tmp/demofile.txt’)

f = open(“/tmp/demofile.txt”, “r”)

r = [{“result”:f.read()}]
return r

You should see the problem.

I get this error
image

However, this works so the file is there.

could you try to avoid using os.system and instead use direct file handling ?

@aya Using os.system is a critical part of what I’m doing. I’m not showing you my actual use case.

Same error here for me.

try:
    # Define the absolute or relative file path
    file_path = "/tmp/test1.txt"  # Replace with the actual file path

    # Open and read the file
    with open(file_path, "r") as file:
        file_content = file.read()

    # Return the file content
    return {
        "file_content": file_content
    }

except FileNotFoundError:
    return {
        "error": f"File not found at path: {file_path}"
    }
except Exception as e:
    return {
        "error": str(e)
    }

Error: File not found at path: /tmp/test1.txt

Inside the container terminal :
~ $ cat /tmp/test1.txt
111

Any thoughts from anyone ?

Thank you

Has anyone been able to look into this?

As per Code node documentation | n8n Docs,

You can’t access the file system or make HTTP requests. Use the following nodes instead:

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