5

I have a script that, when run, creates a directory inside /home/test/ and then writes some files in it. When I run this script, it works fine. However, when I call it from a perl script with

$ret = `/home/..../testscript.py`

it doesn't have permissions so it can't create the folder, or can't write inside it after it is created. It looks like when Python does open("/home/test/abcde/file1.txt", "w"), that file has permissions -rw-r--r--

What can I do to get around this? Is there a way to set /home/test to recursively make all subdirectories have global write-access? Or a better solution maybe?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Andrew Latham
  • 5,982
  • 14
  • 47
  • 87

1 Answers1

12

Put:

os.umask(0000)

in the Python script before it creates the directory. If you can't change the Python script, put:

umask(0000)

in the Perl script before it calls the Python script.

When a new file or directory is created, the permissions are determined by taking the permissions specified in the call to creat() or mkdir(), and then masking off the bits that are specified in the umask.

Typically, applications specify 0666 or 0777 permissions when they call the function (depending on whether they're creating something that should be executable or not). A common value for umask is 022, which turns off group and world write permissions. If you don't want them turned off, use the above umask value to keep the permissions specified in the call.

Barmar
  • 741,623
  • 53
  • 500
  • 612