Import OS, SYS # assumed /tmp/foo.txt file exists, and read and write permissions RET = os.access ( " /tmp/foo.txt " , os.F_OK) Print ( " F_OK - returns the value S% " % RET) RET = os.access ( " /tmp/foo.txt " , os.R_OK) Print ( " R_OK - returns the value S% " % RET) RET = os.access ( " /tmp/foo.txt " , OS .W_OK) Print ( " W_OK - returns the value S% " % RET) RET = os.access (" /Tmp/foo.txt " , os.X_OK) Print ( " X_OK - returns the value S% " % RET)
Import os, SYS path = " / tmp " # to view the current working directory retval = os.getcwd () Print ( " current working directory is S% " % retval) # change the current directory os.chdir (path) # After a review of modified working directory retval = os.getcwd () Print ( " directory successfully modified S% " % retval)
Import OS, STAT path = " /tmp/foo.txt " # file flag is set, so that it can not be deleted, and renamed the flags = stat.SF_NOUNLINK retval = os.chflags (path, the flags) Print ( " Return Value:% S " % retval)
Import OS, SYS, STAT # assuming /tmp/foo.txt file exists, the setting file can be performed by the user group os.chmod ( " /tmp/foo.txt " , stat.S_IXGRP) # setting file can be written to other users os.chmod ( " /tmp/foo.txt " , stat.S_IWOTH) Print ( " modified successfully! " )
Import os, SYS # assumed /tmp/foo.txt file exists. # Set the owner ID to 100 os.chown ( " /tmp/foo.txt " , 100, -1 ) Print ( " Modify permission success !! " )
Import os, SYS # Set the root directory to / tmp os.chroot ( " / tmp " ) Print ( " Modify the root of success !! " )
Import os, SYS # open the file fd = os.open ( " foo.txt " , os.O_RDWR | os.O_CREAT) # write string os.write (fd, " This IS the Test " ) # close the file os.close (fd) Print ( " close the file success! " )
Import os, SYS # open the file fd = os.open ( " foo.txt " , os.O_RDWR | os.O_CREAT) # write string os.write (fd, " This IS the Test " ) # close the file os.closerange (fd, fd) Print ( " close the file success! " )
Import OS, SYS # switch to the "/ var / www / html" directory of the os.chdir ( " / var / WWW / HTML " ) # Print directory Print ( " current working directory:% S " % The os.getcwd ()) # open the "/ tmp" FD = os.open ( " / tmp " , os.O_RDONLY) # use os.fchdir () method to modify the directory os.fchdir (FD) # Print directory Print ( " current working directory:% s " % os.getcwd ()) # close the file os.close (fd)
Import OS, SYS # open the file FD = os.open ( " f1.txt " , os.O_RDWR) # reads text RET = os.read (FD, 12 is ) Print (RET) # close the file os.close (fd) Print ( " close the file success! " )
Import OS the src = ' / usr / bin / Python ' DST = ' / tmp / Python ' # create the soft link os.symlink (the src, DST) # soft link display source link path = os.readlink (DST) Print (path )
Import os, SYS # listed in the directory print ( " directory:% S " % os.listdir (os.getcwd ())) # remove os.remove ( " aa.txt " ) # directory listing after removing the print ( " after removal:% S " % the os.listdir (the os.getcwd ()))
Import os, SYS # listed in the directory Print ( " directory:% S " % os.listdir (os.getcwd ())) # rename os.rename ( " the Test " , " test2 " ) Print ( " Rename successful . " ) # list directory renamed Print ( " directory:% S " % os.listdir (os.getcwd ()))
os Mport, SYS # open the file fd = os.open ( " f1.txt " , os.O_RDWR | os.O_CREAT) # write the string str = " This IS runoob.com Site " RET = os.write (fd, bytes (STR, ' UTF-. 8 ' )) # input return value Print ( " number of bits written is: " ) Print (RET) Print ( " successfully written " ) # close the file os.close (FD) Print ( " Close the file success! " )
Import os Print (os.path.basename ( ' /root/runoob.txt ' )) # Returns the filename Print (os.path.dirname ( ' /root/runoob.txt ' )) # Returns the directory path Print (os. path.split ( ' /root/runoob.txt ' )) # split the file name and path Print (the os.path.join ( ' the root ' , ' Test ' , ' runoob.txt ' )) # directory and filename synthesis a path
Import os Import Time File = ' /root/runoob.txt ' # file path print (os.path.getatime (File)) # output last access time print (os.path.getctime (File)) # output file creation time print (os.path.getmtime (File)) # output last modification time Print (time.gmtime (os.path.getmtime (File))) # output struct_time recently modified form Print (os.path.getsize (File)) # output file size (in bytes) Print (os.path.abspath (file)) # print absolute Print (os.path.normpath (file)) # Specification path string