Chapter VI Common module (3): python common module (os, sys)

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.
  1. os.getcwd () to get the current working directory, the directory path to the current level of python scripts work

  2. os.listdir () returns the name of all files and directories under the specified directory

  3. os.remove () is used to delete a file

  4. os.removedirs (r'c: \ python ') Delete multiple directories

  5. os.system () run shell commands

  6. os.getenv ( 'HOME') operating system reads the value of environment variable HOME

  7. os.environ
    return all of the operating system environment variables

  8. os.environ.setdefault ( 'HOME', '/ home / alex') set system environment variables, is only valid when program running

  9. os.linesep platform gives the current line terminator (windows using '\ r \ n', linux, and mac use '\ n')

  10. os.name only platform you use (windows: nt, linux / unix: posix)

  11. os.rename (old, new) renamed. (Windows systems os.replace (old, new))

  12. os.makedirs (r'c: \ python \ test1 \ test2 ') to create multi-level directory

  13. os.mkdir ( 'test1') to create a single directory

  14. 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)
  15. os.chmod (file) to modify file permissions and timestamps

  16. os.chdir (dirname) change the working directory to the dirname

  17. os.get_terminal_size()

  18. os.kill ()
2. os.path.
  1. os.path.isfile () path to test whether a given file. Return bool

  2. os.path.isdir () test whether a given path is a directory. Return bool

  3. os.path.isabs () to determine whether it is an absolute path. ? ? ? ? A bit difficult to use ah

  4. Os.path.exists Path () test whether a given exists. Return bool

  5. 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')

  6. 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 ',' ')
    `` `

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

  8. 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__))) # 打印当前脚本所在目录的路径

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

  10. os.path.getsize (file) Get File Size

  11. os.path.join (dir, filename) splicing directory name and file name

3. sys.
  1. sys.argv command line parameters List, the first element is the path to the program itself.

  2. sys.exit (n) to exit the program, when the normal exit exit (0)

  3. sys.version print python interpreter version

  4. sys.maxsize
    maximum int value of python print

  5. sys.path returns a list of module search path. You can append the path, but after the end of the program will restore the default.

  6. sys.platform return to the operating system platform name (windows: win32, linux: centos, etc., mac: darwin)

  7. sys.stdout.write ( 'please') standard output,

  8. sys.stdin.readline () [: - 1] standard input, similar to input ()

  9. sys.getrecursionlimit () recursively to maximize the number of layers. 1000 default layer

  10. sys.setrecursionlimit (n) set the maximum recursion layers

  11. sys.getdefaultencoding () Gets the default encoding interpreter

  12. sys.getfilesystemencoding () Gets save memory data to a file in the default encoding

Guess you like

Origin www.cnblogs.com/py-xiaoqiang/p/11110853.html