pythonextractroottartarfile

How to set root privilege for tarfile module in python?


This is my code:

gitlab_restore = tarfile.open("PATH/gitconf.tar")
gitlab_restore.extract("/opt/destination/path")
gitlab_restore.close()

When I run it this error occurred

2021-11-07 09:44:37,139 - root - ERROR - [Errno 13] Permission denied: '/opt/destination/path'

How can I set root permission for the tarfile module to write on /opt?


Solution

  • You can't. Instead, the user that runs the script should have root privileges. Something like this in your example:

    sudo python my_extractor.py