Survey banner
Switching to Dataiku - a new area to help users who are transitioning from other tools and diving into Dataiku! CHECK IT OUT

Read a file line by line, in Python when read in as bytes

Level 3
Read a file line by line, in Python when read in as bytes



I am trying to read in a config.txt file stored in a local managed folder.



with input_folder.get_download_stream("config.txt") as f:
     data =



However when I try and iterate over the text file to read it in, I get an error as it is a bytes file and python doesnt see it as a file.



line = data.read_line()
cnt = 1
while line:
    print("Line {}: {}".format(cnt, line.strip()))
    line = data.readline()
    cnt += 1



How do I convert this to a file format I can read in, in a traditional python way?  This generates the error:


AttributeError: 'bytes' object has no attribute 'read_line'


0 Kudos
2 Replies

Hi @indy2005 

Here's a quick example. Please let me know if you have follow up questions or if I have misunderstood your question.

I have a file called `config.txt` in a managed folder with id `kiOwqj8S`.

Contents of config.txt:

cat config.txt
config_a = 1
config_b = 2
config_c = 3

In a Python notebook in DSS I run the following code:

import dataiku

folder = dataiku.Folder("kiOwqj8S", project_key="COMMUNITY_25416")

with folder.get_download_stream("/config.txt") as stream:
    Lines = stream.readlines()
    count = 1
    for line in Lines:
        print("Line{}: {}".format(count, line.strip()))
        count += 1


Line1: b'config_a = 1'
Line2: b'config_b = 2'
Line3: b'config_c = 3'

Including link to documentation in case helpful:



Level 3



This is useful thanks!!!





0 Kudos