Python OS module

Source: Internet
Author: User

#OS模块
#os模块就是对操作系统进行操作, the module must be imported first: Import OS#GETCWD () Gets the current working directory (the current working directory is the folder where the current file is located) result = OS.GETCWD () print (Result) # ChDir () Change current working directory Os.chdir ('/home/sy ') result = OS.GETCWD () print (result) open (' 02.txt ', ' W ') # If you write the full path when you do not need to consider the default working directory problem, according to the actual writing path operation open ('/home/sy/download/02.txt ', ' W ') #listdir () Gets the name list of all contents in the specified folder result = Os.listdir ('/home/sy ') print (Result) #mkdir () creates a folder #os.mkdir (' Girls ') #os. mkdir (' Boys ', 0o777) #makedirs () recursively Create folders # 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 () Execute system command (dangerous function) # result = Os.system (' ls-al ') #获取隐藏文件 #print (Result) #环境变量 "environment variable is a collection of commands that 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 variable of the system result = os.getenv (' PATH ') print (Result.split (': ')) #putenv () adds a directory to the environment variable (temporarily increasing only valid for the current script) #os. putenv (' PATH ', '/home/sy/download ') #Os.system (' syls ') #exit () The command that exits the terminal #os a common value in the module #curdir represents the current folder. Indicates that the current folder can typically omit print (Os.curdir) #pardir represent the previous level of folder: Indicates that the previous level folder cannot be omitted!print (Os.pardir) #os. mkdir ('.. /.. /.. /man ') #相对路径 find #os.mkdir ('/home/sy/man1 ') starting from the current directory #绝对路径 find #name from the root to get the name string that represents the operating system print (Os.name) #posix Linux or Unix System NT-Window System #sep get the system path interval symbol window->\ linux->/print (os.sep) #extsep get the interval symbol between the file name and the suffix Windo W & Linux. Print (OS.EXTSEP) #linesep get the line break symbol for the operating system window, \ r \ n Linux/unix, \nprint (repr (OS.LINESEP)) # Importing the OS module import os# The following are the contents of the Os.path submodule #abspath () Convert the relative path to the absolute path Path = './boys ' #相对result = Os.path.abspath (path) print ( Result) #dirname () Gets the directory part of the full path & BaseName () Gets the full path of the body part of Path = '/home/sy/boys ' result = os.path.dirname (path) Print (Result) result = Os.path.basename (path) print (result) #split () cuts a complete path into the directory part and the body part path = '/home/sy/boys ' result = Os.path.split (path) print (result) #join () merges 2 paths into one var1 = '/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 the file path = '/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 () detect if the file is Path = '/home/ sy/000.py ' result = os.path.isfile (path) print (result) #isdir () detect if the folder result = Os.path.isdir (path) print (Result) # Islink () detects if the link path = '/initrd.img.old ' result = Os.path.islink (path) print (result) #getctime () Gets the creation time of the file get create times #getmtime () Get File modification time get modify time#getatime () get the file access time get active timeimport timefilepath = '/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 () detects if a path exists filepath = '/home/sy/download/chls ' result = os.path.exists (filepath) print (result) #isabs () detects if a path is an absolute path, path = '/boys ' result = os.path.isabs (path) print ( Result) #samefile () detects if 2 paths are the same file path1 = '/home/sy/download/001 ' path2 = '. /.. /.. /download/0"result = Os.path.samefile (path1,path2) print (result) #os. Environ used to get and set built-in values for system environment variables import os# get system environment Variables getenv () Effect Print (os.environ[' path ') #设置系统环境变量 putenv () os.environ[' path '] + = ':/home/sy/download ' Os.system (' CHLs ')

Python OS module

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.