YouTip LogoYouTip

Os Access

# Python2.x Python os.access() Method [![Image 3: Python File Methods](#) Python OS File/Directory Methods](#) * * * ### Overview The os.access() method attempts to access the path using the current uid/gid. Most operations use the effective uid/gid, so the runtime environment can attempt access in suid/sgid environments. ### Syntax The syntax for the **access()** method is as follows: os.access(path, mode); ### Parameters * **path** -- The path to check for access permissions. * **mode** -- mode can be F_OK to test for existence, or it can be a combination of R_OK, W_OK, and X_OK, or one or more of them. * **os.F_OK:** As the mode parameter for access(), tests if the path exists. * **os.R_OK:** Included in the mode parameter for access(), tests if the path is readable. * **os.W_OK:** Included in the mode parameter for access(), tests if the path is writable. * **os.X_OK:** Included in the mode parameter for access(), tests if the path is executable. ### Return Value Returns True if access is allowed, otherwise returns False. ### Example The following example demonstrates the use of the access() method: #!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # Assume the /tmp/foo.txt file exists and has read/write permissions ret = os.access("/tmp/foo.txt", os.F_OK)print "F_OK - Return Value %s"% ret ret = os.access("/tmp/foo.txt", os.R_OK)print "R_OK - Return Value %s"% ret ret = os.access("/tmp/foo.txt", os.W_OK)print "W_OK - Return Value %s"% ret ret = os.access("/tmp/foo.txt", os.X_OK)print "X_OK - Return Value %s"% ret Executing the above program outputs the following: F_OK - Return Value True R_OK - Return Value True W_OK - Return Value True X_OK - Return Value False [![Image 4: Python File Methods](#) Python OS File/Directory Methods](#)
← Os ChflagsFile Writelines β†’