Python OS Module

Source: Internet
Author: User

  #OS Module
#The OS module is the operation of the operating system, which must first be imported into the module:ImportOS#GETCWD () Gets the current working directory (the current working directory is the folder in which the current file is located by default)result =OS.GETCWD ()Print(Result)#chdir () change the current working directoryOs.chdir ('/home/sy') Result=OS.GETCWD ()Print(Result) open ('02.txt','W')#when you write a full path, you do not need to consider the problem of the default working directory and follow the actual writing path.Open'/home/sy/Download/02.txt','W')#Listdir () gets a list of names for all content in the specified folderresult = Os.listdir ('/home/sy')Print(Result)#mkdir () Creating a folder#os.mkdir (' Girls ')#os.mkdir (' Boys ', 0o777)#makedirs () to create a folder recursively#os.makedirs ('/home/sy/a/b/c/d ')#rmdir () Delete empty directory#os.rmdir (' Girls ')#removedirs Recursive Delete folder must be empty directory#os.removedirs ('/home/sy/a/b/c/d ')#rename () file or folder rename#os.rename ('/home/sy/a ', '/home/sy/alibaba '#os.rename (' 02.txt ', ' 002.txt ')#stat () Get information about a file or folder#result = Os.stat ('/home/sy/pycharmproject/python3/10.27/01.py)#print (Result)#system () Execution Systems Command (dangerous function)#result = Os.system (' ls-al ') #获取隐藏文件#print (Result)#Environment Variables" "an environment variable is a collection of commands that are set by the operating system environment variable is the set of directories that the operating system searches for commands when executing system commands" "#getenv () Gets the environment variables of the systemresult = Os.getenv ('PATH')Print(Result.split (':'))#putenv () adds a directory to the environment variable (temporary increase is only valid for the current script)#os.putenv (' PATH ', '/home/sy/download ')#os.system (' syls ')#Exit () command to exit the terminal#common values in the OS module#CurDir represents the current folder. Indicates that the current folder can be omitted in generalPrint(Os.curdir)#Pardir represents the previous level of folder: Indicates that the previous level folder must not be omitted!Print(Os.pardir)#Os.mkdir ('.. /.. /.. /man ') #相对路径 search starting from the current directory#os.mkdir ('/home/sy/man1 ') #绝对路径 find from the root directory#Name gets a string of names that represent the operating systemPrint(Os.name)#POSIX-Linux or Unix System NT-Window System#Sep gets system path interval symbol window->\ linux->/Print(OS.SEP)#Extsep Get the interval between file name and Suffix window & Linux.Print(OS.EXTSEP)#Linesep get the line break symbol for the operating system window, \ r \ n Linux/unixPrint(repr (os.linesep))#Import OS moduleImportOS#The following are the contents of the Os.path submodule#Abspath () converts a relative path to an absolute pathPath ='./boys'#relativeresult =Os.path.abspath (path)Print(Result)#dirname () get the full path to the directory section & BaseName () Get the main part of the full pathPath ='/home/sy/boys'result=os.path.dirname (path)Print(Result) result=os.path.basename (path)Print(Result)#split () cuts a complete path into the table of contents and body partsPath ='/home/sy/boys'result=os.path.split (path)Print(Result)#join () merges 2 paths into oneVAR1 ='/home/sy'var2='000.py'result=Os.path.join (VAR1,VAR2)Print(Result)#Splitext () cuts a path into a file suffix and two other parts, primarily for obtaining the suffix of a filePath ='/home/sy/000.py'result=os.path.splitext (path)Print(Result)#GetSize () Gets the size of the file#path = '/home/sy/000.py '#result = os.path.getsize (path)#print (Result)#isfile () detects if it is a filePath ='/home/sy/000.py'result=os.path.isfile (path)Print(Result)#Isdir () detects if it is a folderresult =os.path.isdir (path)Print(Result)#Islink () detect if it is a linkPath ='/initrd.img.old'result=os.path.islink (path)Print(Result)#Getctime () Gets the creation time of the file get create times#Getmtime () Gets the modified time of the file get modify times#Getatime () Gets the access time of the file get active timesImportTimefilepath='/home/sy/Download/chls'result=os.path.getctime (filepath)Print(Time.ctime (result)) result=os.path.getmtime (filepath)Print(Time.ctime (result)) result=os.path.getatime (filepath)Print(Time.ctime (result))#exists () to detect if a path is actually presentfilepath ='/home/sy/Download/chls'result=os.path.exists (filepath)Print(Result)#Isabs () detects if a path is an absolute pathPath ='/boys'result=os.path.isabs (path)Print(Result)#Samefile () detects if 2 paths are the same filepath1 ='/home/sy/Download/001'path2='.. /.. /.. /Download/001'result=os.path.samefile (path1,path2)Print(Result)#Os.environ for getting and setting built-in values for system environment variablesImportOS#Get System environment variable getenv () effectPrint(os.environ['PATH'])#Set System environment variable putenv ()os.environ['PATH'] +=':/home/sy/Download'Os.system ('CHLs')

Python 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.