YouTip LogoYouTip

Python3 Os Access

# Python3.x Python3 os.access() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 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 contexts. ### Syntax The syntax for the **access()** method is as follows: os.access(path, mode); ### Parameters * **path** -- The path to check for access permissions. * **mode** -- The mode can be F_OK to test for existence, or it can include R_OK, W_OK, and X_OK, or one or more of R_OK, W_OK, and X_OK. * **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/python3import 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 produces the following output: F_OK - return value True R_OK - return value True W_OK - return value True X_OK - return value False [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 File WritelinesPython3 File Writelines β†’