Python Basic Learning Log day5---OS module

Source: Internet
Author: User
Tags python script

The Python OS module provides an interface for invoking the operating system.

#-*-Coding:utf-8-*-
__author__ = ' Shisanjun '

Import OS

Print (OS.GETCWD ()) #获取当前工作目录, the directory path where the current Python script works

Os.chdir ("F:\python devops Development \day4") #改变当前的工作目录: equivalent to the shell CD
Print (OS.GETCWD ()) #结果F: \python devops Development \day4

Os.chdir (Os.curdir) #返回当前目录:(.): equivalent to the shell CD.
Print (OS.GETCWD ())

Os.chdir (os.pardir) #返回当前父目录:(...): equivalent to the shell under the CD.
Print (OS.GETCWD ())

Os.makedirs ("Day6/test") #可生成多层递归目录: equivalent to the shell mkdir-r Day6/test, the directory has an error
Os.removedirs ("Day6/test") #删除多层递归目录: equivalent to Shell under RM-RF day6/test

#os. mkdir ("Day6") #可生成单级目录: equivalent to shell mkdir Day6
#os. RmDir ("Day6") #可删除单级目录: equivalent to Shell rm-f Day6

Print (Os.listdir ("F:\python devops Development")) #列出指定目录下的所有文件和子目录, including hidden files, and printed in a list ls-a

Os.chdir ("F:\python devops Development \day6")
#os. Remove ("1.py") #删除文件
#os. Rename ("2.py", "1.py") #重命名文件
Print (Os.stat ("1.py")) # Get file or directory information
Print (OS.SEP) #输出操作系统特定的路径分隔符, win under "\ \", Linux for "/"
Print (OS.LINESEP) #输出当前平台使用的行终止符, win under "\t\n", Linux "\ n"
Print (OS.PATHSEP) #输出用于分割文件路径的字符串, win down for (:)
Print (Os.name) #输出字符串指示当前使用平台. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Ls-l") #运行shell命令, direct display
Print (Os.environ) # Get System environment variables
Print (Os.path.abspath (os.curdir)) #返回path规范化的绝对路径
Print (Os.path.split (' F:\python devops Development \day6\\test1.py ')) #将path分割成目录和文件名二元组返回
Print (Os.path.basename (' F:\python devops Development \day6\\test1.py ')) #返回path的目录. is actually the first element of Os.path.split (path)
Print (Os.path.dirname (' F:\python devops Development \day6\\test1.py ')) #返回path最后的文件名. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)


Print (Os.path.exists ("F:\python devops Development \day6")) #如果path存在, returns True if path does not exist, returns false
Print (Os.path.isabs ("F:\python devops Development \day6") # If path is an absolute path, return True
Print (Os.path.isabs ("Python devops Development \day6"))
Print (Os.path.isfile (' F:\python devops Development \day6\\1.py ')) #如果path是一个存在的文件, returns True. otherwise returns false

Print (Os.path.isdir ("F:\python devops Development \day6")) #如果path是一个存在的目录, returns True. otherwise returns false

Print (Os.path.join (Os.curdir, "1.py")) # Returns a combination of multiple paths, and the parameters before the first absolute path are ignored

Print (Os.path.getatime (os.curdir)) # Returns the last access time of the file or directory pointed to by path, which is a timestamp
Print (Os.path.getmtime (os.curdir)) # Returns the last modification time of the file or directory pointed to by path, which is a timestamp

Python Basic Learning Log day5---OS module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.