Rizan Zaky
Rizan Zaky

Reputation: 4682

How to open and read from an external text file in odoo?

I want to open a .txt file using python in odoo and read its contents. Where should I place this .txt file for it to be opened.

What I tried,

I kept the file in the same directory as models.py and in models.py wrote the code to open the file,

try:
    logFile = open('log.txt', 'r')

but this file is not opened. Please help, where should I keep log.txt for it to be located, or can I set any path for models.py to understand where log.txt is?

Upvotes: 1

Views: 2624

Answers (2)

Rizan Zaky
Rizan Zaky

Reputation: 4682

I just found the solution, I set the absolute path to where the .txt file is found.

import os

path = os.path.expanduser('<absolute path to .txt file>')
logFile = open(path, 'r')

this worked.

Upvotes: 2

Jainik Patel
Jainik Patel

Reputation: 2324

This is Python Code may be help you:

with open("log.txt") as file:   # Use file to refer to the file object
    data = file.read()
    do something with data
    print data

File handling in Python See this link : http://pymbook.readthedocs.org/en/latest/file.html

Upvotes: 0

Related Questions