changing a file's permissions

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Oct 1 22:24:38 EDT 2006


James <oscartheduck at gmail.com> writes:

> I'm writing a script in linux to excercise my python skills and have
> encountered a minor issue.  Writing the script and creating an ouput
> file was simple enough and didn't take too long. However, I don't
> have permissions to execute the file by default.

This is an attribute of the file (an object in the filesystem) which
is checked by the kernel before allowing the file to be
executed. Python has nothing to do with this; if the attributes allow
execution, you can execute it as a program; if not, you can't.

> Now, I could simply chmod 755 the sucker and have done with it

That (or some equivalent filesystem operation to change the attributes
of the file) is the only solution to the problem you present. This is
by design.

> but I want to apply the permissions within the python script if I can.

Since the permissions are used by the operating system kernel to
determine if you have permission execute the file at all, the program
isn't even executing when the decision is made. Change the permission
attributes of the program file (using 'chmod' or something else that
operates on the filesystem object's attributes), then you'll be able
to run it as a program.

Incidentally, if the program is intended to be run from a command
line, it's best to name the file with no '.py' extension. The fact
that a command is implemented in Python is irrelevant to the person
running that command; it also means you can implement it in some other
language later on without changing everything that uses that command.

-- 
 \       "Facts are meaningless. You could use facts to prove anything |
  `\             that's even remotely true!"  -- Homer, _The Simpsons_ |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list