2

The tar.gz created is not having execute rights when transferred on linux after creation.

Goal: File should have full rights once it is taken on linux system.

I am using 'tarfile' library in python to achieve this.

import os
import tarfile
with tarfile.open('test.tar.gz', "x:gz") as tar:
    tar.add("path_to_src_folder", arcname=os.path.basename("path_to_dir", filter=tarinfo.mode(777))


----------------OR-------------------------------------------


import tarfile
def set_permissions(tarinfo):
    tarinfo.mode = 777
    return tarinfo

with tarfile.open('test.tar.gz', "x:gz") as tar:
    tar.add("path_to_src_folder", filter=set_permissions)
Sameer
  • 67
  • 1
  • 1
  • 6

1 Answers1

-1

In Order to change permission of a file you just have to use the below snippet

os.chmod('manage.py', 0666)

While if you have to change the permission of the whole folder , Here below is the snippet to do it recursively.

import os

def change_permissions_recursive(path, mode):
    for root, dirs, files in os.walk(path, topdown=False):
        for dir in [os.path.join(root,d) for d in dirs]:
            os.chmod(dir, mode)
    for file in [os.path.join(root, f) for f in files]:
            os.chmod(file, mode)
change_permissions_recursive('my_folder', 0o777)

In Order to add/override Permission while creating a tar file , The snippet which is in your question should work fine The same have been mentioned in the below link too,

preserving file permission when creating a tarball with Python's tarfile

redhatvicky
  • 1,912
  • 9
  • 8
  • 3
    This is how to change the mode of files. The question was how to store the files in a tarfile with a given mode so when the archive is extracted the files have that mode. – Dan D. Mar 28 '19 at 05:59