Python base (OS module)

os.getcwd()

Role: Get the current path

usage:

    >>> print(os.getcwd())

os.chdir()

Role: Change the current working directory

usage:

>>> print(os.getcwd())

       /

>>> os.chdir(“/usr/local”)

>>> print(os.getcwd())

    /usr/local

# # Return to parent directory

 >>> os.chdir(“..”)

 os.mkdirs ()

 

Role: Create a recursive directory

usage:

    >>> os.makedirs ( "linux / hundreds")

 os.mkdir()

 

Role: New directory, can only build one

usage:

    >>> os.mkdir(“linux”)

 os.removedirs()

Role: delete the specified directory, if the specified directory is empty deleted, if the parent directory is also empty also removed if the parent directory is not empty is not deleted

usage:

    >>> os.removedirs(“centos/linux”)

 os.rmdir ()

Role: Only single-level directory delete empty folders

usage:

     >>> os.rmdir(“linux”)

 os.listdir ()

Role: the specified folder lists all of the following folders and files, including hidden files , to the list of ways to print out

usage:

    >>> print(os.listdir(“/usr/local/”))

 os.remove ()

Role: delete the specified file  

usage:

    >>> os.remove(“/usr/local/fan.sh”)

os.rename()

Role: Edit folder name or file name

usage:

    >>> os.rename(“hehe.sh”,”xixi.sh”)

os.state ()

Role: View details of a file

usage:

    >>> print(os.state(“/usr/local/hehe.sh”))

    # There timestamp issue (st_mtime = 1528552906)

    # Converted by the time module

    # Time_local = time.localtime (1528552906) # time stamp is converted to a structured

    #print (time.strftime ( "% Y-% m-% d% X", time_local)) # structured into a string of time period

os.sep

 

The role of output current operating system path separator (Linux is "/", Windows is "\")

usage:

    >>> print(os.sep)

 os.system()

Role: Run the terminal command

usage:

    >>> os.system ( "cd / usr / local") # take such output results of less than

    -----------------------------------------------------------------------------------------

    >>> output = os.popen('cd / && ls')

    >>> print (output.read ()) # output back to the car

    -----------------------------------------------------------------------------------------

    >>> date1 = commands.getstatusoutput("date +'%Y-%m-%d-%H-%M'")

    >>> date = str (date1 [1]) # correct result output

os.path.exists()

Action: determining whether there is a path exists is True, False is absent

usage:

    >>> print(os.path.exists(“/usr/local/”))

os.path.isfile()

Role: to determine whether a file exists, exists is True, otherwise False

usage:

    >>> print(os.path.isfile(“/usr/local/hehe.sh”))

os.path.isdir ()

Role: to determine whether there is a file directory, presence is True, it does not exist to False

usage:

    >>> print(os.path.isdir(“/usr/local/”))

os.path.join()

Role: Path splicing (important)

usage:

    >>> a=”/usr/”

    >>> b=”local/hehe.sh”

    >>> print(os.path.join(a,b))

Published 62 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41674452/article/details/103991545