1

I am trying to open a new file and immediately give it full system read/write/execute permissions. I am doing this like so:

csvout = open("/Users/me/Google Drive/documentation/report.csv","w")
os.chmod("/Users/me/Google Drive/documentation/report.csv", 0o777)

This command works for me and another coworker (who runs it from her account: /Users/coworker/, but saves it to the directory in my account, which I have given full read/write permissions to). However, a second coworker receives an error when he reaches the os.chmod line, because he apparently does not have permissions to use chmod on the file.

Sometimes the report.csv file exists prior to running and sometimes it does not, but I don't see how that would change anything since it gets overwritten by opening the file with the "w" option anyway.

What could be going wrong that is causing only certain users to not be able to use the chmod command when the parent directory has full system privileges enabled?

cadams
  • 1,299
  • 1
  • 11
  • 21

1 Answers1

1

Only the owner of a file (or the superuser) is allowed to change its permissions. When the second user runs the script, they aren't the owner (the first user is), so they get an error.

The first user to run the script will create the file, and then give it 777 permissions, which allows anyone to write to the file.

The next user will then be able to open the file for writing. When they try to use os.chmod() they'll get an error because they aren't the file owner. But since it already has the desired permissions, this shouldn't be a problem.

Use a try block around the call to os.chmod() to ignore the error.

Barmar
  • 741,623
  • 53
  • 500
  • 612