pythonlinuxunixtemporary-directory

How do I let "TemporaryDirectory" in python inherit the parent directory's permission?


I'm creating a temporary directory in python using from tempfile import TemporaryDirectory. How do I create a TemporaryDirectory in python and force its permission to be the same as the folder that it is created in?

Unfortunately, the items in TemporaryDirectory are not accessible by another program I have running. This program does have access to TemporaryDirectory's parent directory but not TemporaryDirectory.


Solution

  • I can't see support for it directly in TemporaryDirectory, and indeed the mode 0o700 is hard-coded into the tempfile package, but you could use chmod. Example:

    with TemporaryDirectory(dir=parent) as tmpdir:    
        os.chmod(tmpdir, os.stat(parent).st_mode)
    
        os.system("ls -ld " + parent)
        os.system("ls -ld " + tmpdir)
    

    The question only asked for permissions, but if you also require changing ownership (user or group), then look at os.chown and the st_uid and st_gid properties of the stat_result object (analogous to st_mode above). Note that some operations may require root privileges.