With open ('media file from data table', 'r') Errors

What I’m trying to do:
I am trying to open a .txt file that is located in my data table.

What I’ve tried and what’s not working:
I wrote a function called get_file to get the row, then pass the media file to another function to open.

I get an error saying TypeError: expected str, bytes or os.PathLike object, not LazyMedia

Can anyone explain how I am supposed to pass in the bytes or object?

I would really like to see a full video on how to deal with media files from client side, server side and /tmp side. I cannot understand how Anvil deals with files differently compared to desktop scripts.

Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 
def get_file(file_name):
  row = app_tables.faults_files.get(file_name= file_name)
  return row['file_media']

# Different server module calling get_file
 file_media = get_file('Faults Routine')
 with open(file_media, 'r') as file:
     header = file.read()

probably returns a Media Object, not a filename. This kind of object has a get_bytes() function. See

Hey Phil,

Thank you for the reply.

When I use the (file_media.get_bytes(), ‘r’) function. It tries to place the entire contents of the file as the ‘name’, and if I use the (file_media.name, ‘r’) I get a cannot find file with this name.

Is there a way to pass the file as an object? or should I make a file locally in the /tmp folder and open it that way?

get_bytes() should probably be used in place of file.read(). That is, it is the contents of the file, as if you had read it from disk.

1 Like

Ahh I see you are skipping opening the file since get_bytes() returns the contents anyways.

Let me try that out… Thank you for the suggestion.