"Python" Learning note 4-os, sys module

Source: Internet
Author: User

1. OS Operating system module

1 ImportOS2 Print(OS.GETCWD ())#get current working directory3 #Print (Os.chdir (".")) #更改当前目录. Current directory: Parent Directory4 #print (os.curdir) #当前目录, relative path5 #print (os.pardir) #父目录, relative path6 #Print (Os.mkdir (' test1 ')) #创建文件夹7 Print(Os.rmdir ('d:/logs/c_/temp'),'Sdfasfasfadfaasdfad')#delete folders, only empty folders can be deleted8 #Print (Os.remove ('.. /day4/aaa.txt ')) #删除文件 cannot delete the folder9 Print(Os.listdir ('D:/zdx-code/day5'))#lists all files in the current directory, returns all folder names under the current directory listTen #Print (Os.rename (' sss.txt ', ' bbb.txta ')) #更改文件名 One #Print (Os.stat (' Notes ')) #获取文件信息 A #print (__file__) #获取当前python文件的绝对路径 - #Print (Os.path.abspath (__file__)) #获取绝对路径???  - #Print (Os.path.split ("C:\\usr\\hehe\\hehe.txt")) #分割路径和文件名 the #Print (Os.path.dirname ("C:\\usr\\hehe\\hehe.txt")) #获取父级目录 - #Print (Os.path.basename ("C:\\usr\\hehe\\hehe.txt")) #获取最后一级文件 - #Print (os.path.exists ("/usr/local")) # Directory/file exists - #Print (Os.path.isabs (".")) # Determine if it is an absolute path + #Print (Os.path.isfile ("/usr/local")) # to determine if it is a file - #Print (Os.path.isdir ("/usr/local")) # is a path + #Print (Os.path.join ("/root", ' hehe ', ' A.sql ')) # stitching into a path #使用频率高 A #Print (Os.path.getatime ("len_os.py")) # Output last access time at #Print (Os.path.getmtime ("len_os.py")) # Output last access time - # - #path1= R ' e:\\txt\day\\data ' #r ' do not recognize \ t this tab - #print (OS.SEP) # Path delimiter for the current operating system - #print (OS.LINESEP) # line break for the current operating system - #print (OS.PATHSEP) # The delimiter for each path in the environment variable of the current system, Linux is:, Windows is; in #print (Os.environ) # Environment variables for the current system - #print (os.name) # Current system Name window shows NT Linux display POSIX
 # above the key record, the job has something to do with it:
Print (OS.GETCWD ())#取当前工作目录
# Print (Os.mkdir ("Test1") #) # Create Folder
# Print (Os.remove (". /day4/5.png ") # Delete the file, you cannot erase the folder.
# Print (Os.listdir (' c://')) # list all files in one directory
# os.rename ("Test", "Test1") # Rename
# Print (Os.path.abspath (__file__)) # Get absolute path
# Print (Os.path.split ("C:\\usr\\hehe\\hehe.txt")) # Split path and file name
# Print (Os.path.dirname ("C:\\usr\\hehe\\hehe.txt")) # Get Parent Directory
# Print (os.path.exists ("C://test2")) # Directory/file exists
Print (Os.path.isfile (R "D:\meng\BestTest\code\zdx-code\day5"),' Ssssssssssssssssssssssssssssssssss ')#判断是否是一个文件
Print (Os.path.isdir (R "D:\meng\BestTest\code\zdx-code\day5"))#是否是一个文件夹
Print (Os.path.join ("E",' S.txt '))#拼接成一个路径


Os.system (' dir ') #只能用来执行操作系统命令, cannot get results
# #比如执行Linux系统命令os. System (' Ifconfig ')
Res = Os.popen (' ipconfig ') #执行操作系统命令, can get the mountain return value, use Read ()
Print (Res.read ())

# Linux Top Dynamic View performance, not with the above popen, all dynamic commands are not available, but Top-n 1 only once, you can use


2. SYS module
1 #Constants2 #Import SYS3 #print (sys.path) #环境变量4 #print (sys.platform) #看当前系统是什么系统5 #print (sys.version) #查看当前python的版本6 #print (SYS.ARGV) #是运行python文件时, gets the parameter value passed in (command line: Python filename parameter value 1 parameter value 2), (Command line argument list, first element is the program itself path) View Practice 7clear_log.py 7 #sys.exit () #退出程序, exit Normal exit (0)8 #quit (' program quit ') #跟上面一样, do not execute the following code9 #sys.version #获取Python解释程序的版本信息Ten #Sys.maxint #最大的Int值 One #Sys.path #返回模块的搜索路径, using the value of the PYTHONPATH environment variable when initializing A #Sys.platform #返回操作系统平台名称 - #sys.stdout.write (' please: ') #向屏幕输出一句话 - #val = Sys.stdin.readline () [: -1] #获取输入的值

3. Practice Clean_log

Practice Argvs, run with terminal terminal (run default without arguments directly), enter: Python filename parameter value 1 parameter value 2 empty a log file

1 ImportSys,os2 defClean_log (path):3     Print("The log is empty .")#the contents of the job supplement4 5 defback_db (db_name):6     Print("database has been backed up")7 8args = sys.argv#Argvs is run python file name parameter 1 parameter 2, list[filename, parameter 1, parameter 2, parameter 3]9 Print(args)Ten ifLen (args) >1: OnePath = Args[1] Adbname = args[2] - back_db (dbname) -     ifOs.path.isdir (path): theClean_log (args[1]) -     Else: -         Print('path does not exist! ') - Else: +     Print('running this python file requires passing in a path \ n' -           'e.g Python Clean_lpy.py/usr/tomcat/logs')

"Python" Learning note 4-os, sys 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.