python module--os module, sys module

1. os module

1 os.getcwd() Get the current working directory, that is, the directory path where the current python script works
 2
 3 os.chdir("dirname") changes the working directory of the current script; equivalent to cd under the shell
 4
 5 os.curdir returns the current directory: ('.')
 6
 7 os.pardir Get the current directory string name: ('..')
 8
 9 os.makedirs('dirname1/dirname2') can generate multi-level recursive directories
10
11 os.removedirs('dirname1') If the directory is empty, delete it; and recurse to the previous directory, if it is also empty, delete it,
12 and so on
13                       
14 os.mkdir('dirname') Generate a stand-alone directory; equivalent to shell mkdir dirname
15
16 os.rmdir('dirname') Delete a single-level empty directory. If the directory is not empty, it cannot be deleted, and an error is reported; equivalent to
17                      shell种rmdir  dirname
18                      
19 os.listdir('dirname') List all files and sub-file directories in the specified directory, including hidden files, and print them in a list
20
21 os.remove() removes a file
22
23 os.rename("oldname","newname") rename file/directory
24
25 os.stat('path/filename') Get file/directory information
26
27 os.sep output the path separator specific to the operating system, "\\" under win, "\n" under Linux
28
29 os.linesep The string used to split the file path is ';' under win, and ':' under Linux
30
31 os.name Output string Knowledge of the current platform in use. win -> 'nt';Linux -> 'posix'
32
33 os.system("bash command") Run the shell command and display it directly
34
35 os.environ Get system environment variables
36
37 os.path.abspath(path) returns the normalized absolute path of path
38
39 os.path.split(path) split path into directory and file name two-tuple return
40
41 os.path.dirname(path) Returns the directory of path. In fact, it is the first element of os.path.split (path)
42
43 os.path.basename(path) Returns the last filename of path. If the path ends with / or \, then it will return a null value, i.e.
44 The second element of os.path.split(path)
45                         
46 os.path.exists(path) If path exists, return True; if path does not exist, return False
47
48 os.path.isfile(path) If path is an existing file, return T to enter Russian, otherwise return False
49
50 os.path.isabs(path) If it is an absolute path, return True
51
52 os.path.isdir(path) If path is an existing directory, return True, otherwise return False
53
54 os.path.join(path1[,path2[,...]]) Returns after combining multiple paths, the parameter before the first absolute path
55 will be ignored
56
57 os.path.getatime(path) Returns the last access time of the file or directory desired by path
58
59 os.path.getmtime(path) Returns the last modification time of the pointed file or directory

 

 1 import os
 2 # print(os.getcwd())       #获取该脚本当前的目录路径
 3 # os.chdir('..')     #去当前目录的上一级目录路径
 4 # print(os.getcwd())
 5 
 6 
 7 print(os.path.split(r"C:/python36练习题/day22/day22.py"))#将该路径与文件名切割,一元组形式打印
 8 print(os.path.dirname(r"C:/python36练习题/day22/day22.py" )) #返回 该文件所在文件夹路径
 9 print(os.path.basename(r"C:/python36练习题/day22/day22.py")) # 返回该文件名
10 a = 'C:/python36练习题'
11 b = 'day22/day22.py'
12 print(os.path.join(a,b))  #路径拼接
View Code

 

二、sys模块

1 sys.argv    命令行参数List,第一个元素是程序本身路径
2 sys.exit(n)  退出程序,正常退出时exit(0)
3 sys.version   获取python解释程序的版本信息
4 sys.maxint   最大的int值
5 sys.path    返回模块的搜索路径,初始化时使用pythonpath 环境变量的值
6 sys.platform  返回操作系统平台名称                      
7                                   
 1 import sys
 2 print(sys.argv)
 3 
 4 command=sys.argv[1]
 5 path=sys.argv[2]
 6 
 7 if command=="post":
 8     pass
 9 
10 elif command=="get":
11     pass
12 
13 import time
14 for i in range(100):
15     sys.stdout.write("#")
16     time.sleep(0.1)
17     sys.stdout.flush()
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325521750&siteId=291194637