CD9999
CD9999

Reputation: 109

Using Python to upload a binary file to a postgres server

I am trying to upload a binary file to a postgres server. I think I correctly modified a Python script (https://gist.github.com/illume/8aa7c2d03273da846bf4361236a4f3f9#file-image_save-py) so that it will connect to my postgres server, but I am stuck trying to invoke the script in the command line. From what I understand, I need to navigate to the directory where I have the image_save.py script and call it. I also put a test file in the same directory, so I attempted to execute:

python image_save.py shuttle.jpg

For which I got this error: "error: one of the arguments --store --fetch is required."

I then tried:

python image_save.py --store shuttle.jpg

I got:

  Traceback (most recent call last):
  File "image_save.py", line 45, in <module>
    main(sys.argv)
  File "image_save.py", line 30, in main
    curs.execute("INSERT INTO files(id, orig_filename, file_data) VALUES (DEFAULT,%s,%s) RETURNING id", (args.filename, filedata))
ValueError: A string literal cannot contain NUL (0x00) characters.

I also tried:

python -m image_save.py shuttle.jpg

And I got "C:\Python27\python.exe: No module named image_save.py"

I really think I am just invoking the script incorrectly. Can anyone see what I am doing wrong?

Upvotes: 5

Views: 3014

Answers (1)

Liam
Liam

Reputation: 6439

It is because your file contains NULL characters and that causes the error and it looks like Postgres doesn't allow these characters

To solve this problem just remove those bad characters from your file with this simple script.

fi = open('shuttle.jpg', 'rb')
data = fi.read()
fi.close()
fo = open('new_shuttle.jpg', 'wb')
fo.write(data.replace('\x00', ''))
fo.close()

Upvotes: 2

Related Questions