This article, we introduce module os and sys modules. The main systems are some of the features and interaction of these two modules.
import os
import sys
1. them.
os.getcwd () to get the current working directory, the directory path to the current level of python scripts work
os.listdir () returns the name of all files and directories under the specified directory
os.remove () is used to delete a file
os.removedirs (r'c: \ python ') Delete multiple directories
os.system () run shell commands
os.getenv ( 'HOME') operating system reads the value of environment variable HOME
os.environ
return all of the operating system environment variablesos.environ.setdefault ( 'HOME', '/ home / alex') set system environment variables, is only valid when program running
os.linesep platform gives the current line terminator (windows using '\ r \ n', linux, and mac use '\ n')
os.name only platform you use (windows: nt, linux / unix: posix)
os.rename (old, new) renamed. (Windows systems os.replace (old, new))
os.makedirs (r'c: \ python \ test1 \ test2 ') to create multi-level directory
os.mkdir ( 'test1') to create a single directory
os.stat (file) access to file attributes.
import os d = os.stat(r'C:\Users\xxx\Desktop\python\temp') print(d)
Results of the:
os.stat_result(st_mode=16895, st_ino=4222124650888181, st_dev=508102, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1561633541, st_mtime=1561633541, st_ctime=1559978342)
os.chmod (file) to modify file permissions and timestamps
os.chdir (dirname) change the working directory to the dirname
os.get_terminal_size()
- os.kill ()
2. os.path.
os.path.isfile () path to test whether a given file. Return bool
os.path.isdir () test whether a given path is a directory. Return bool
os.path.isabs () to determine whether it is an absolute path. ? ? ? ? A bit difficult to use ah
Os.path.exists Path () test whether a given exists. Return bool
os.path.split (path) to the parent directory and files (folders) apart and returns a tuple. split and can not distinguish between a directory or file, it will only last layer folder name or file name ripped out
python import os d = os.path.split(r'C:\Users\xxx\Desktop\python\temp') # temp是一个文件夹 print(d) # 输出结果:('C:\\Users\\xxx\\Desktop\\python', 'temp')
os.path.splitext (file) separate extension, return tuple.
Python `` `
Import OS
D = os.path.splitext (r'C: \ the Users \ XXX \ Desktop \ Python \ temp.txt ') # returns (' path ',' Extension ') tuple
print ( d)d = os.path.splitext (r'C: \ Users \ xxx \ Desktop \ python \ temp ') # no extension will return a null character in the extension portion
Print (D)
执行结果:
(' C: \ the Users \ XXX \ Desktop \ Python \ TEMP ',' .txt ')
(' C: \ the Users \ XXX \ Desktop \ Python \ TEMP ',' ')
`` `os.path.dirname (file) Gets the parent directory path (parent directory) path, and returns string.
python import os d = os.path.dirname(r'C:\Users\xxx\Desktop\python\temp') print(d) # 输出结果:C:\Users\kouneli\Desktop\python
os.path.abspath () Gets absolute path, and returns the string.
python import os print(__file__) # 环境变量'__file__':当前脚本的相对路径,包含文件名 print(os.path.abspath(__file__)) # 打印当前脚本的绝对路径 print(os.path.dirname(os.path.abspath(__file__))) # 打印当前脚本所在目录的路径
os.path.basename (file) access to the most subordinate file (folder) name.
python import os d = os.path.basename(r'C:\Users\xxx\Desktop\python\temp') print(d) # 输出结果:temp
os.path.getsize (file) Get File Size
os.path.join (dir, filename) splicing directory name and file name
3. sys.
sys.argv command line parameters List, the first element is the path to the program itself.
sys.exit (n) to exit the program, when the normal exit exit (0)
sys.version print python interpreter version
sys.maxsize
maximum int value of python printsys.path returns a list of module search path. You can append the path, but after the end of the program will restore the default.
sys.platform return to the operating system platform name (windows: win32, linux: centos, etc., mac: darwin)
sys.stdout.write ( 'please') standard output,
sys.stdin.readline () [: - 1] standard input, similar to input ()
sys.getrecursionlimit () recursively to maximize the number of layers. 1000 default layer
sys.setrecursionlimit (n) set the maximum recursion layers
sys.getdefaultencoding () Gets the default encoding interpreter
sys.getfilesystemencoding () Gets save memory data to a file in the default encoding