5

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?

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Is this a serious suggestion?
But if you delete the null bytes then the data is irreversibly altered. Making it useless?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.