#16 os&sys模块
在编写Python程序的时候,可能需要和操作系统进行交互,比如获取某个目录下的内容、更改运行目录、更改环境变量等操作。在Python中有两个模块将这些问题完美解决,一起看看哇!
一、os模块
os模块提供一个和操作系统的接口,想要完全了解它,需要查看它的官方文档:https://docs.python.org/3.5/library/os.html
这里列出一些常用的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | In [3]: import os        
  In [4]: os.sep            Out[4]: '/'
  In [5]: os.linesep        Out[5]: '\n'
  In [6]: os.pathsep        Out[6]: ':'
  In [7]: os.name           Out[7]: 'posix'
  In [8]: os.environ       
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | In [9]: import os
  In [10]: os.getcwd()                           Out[10]: '/Users/minutesheep'
  In [11]: os.pardir                             Out[11]: '..'
  In [12]: os.curdir                             Out[12]: '.'
  In [13]: os.mkdir('test')                     
  In [15]: os.makedirs('test1/test2/test3')     
  In [16]: os.listdir()                         
  In [17]: os.rmdir('test')                     
  In [19]: os.removedirs('test1/test2/test3')   
  In [20]: os.chdir('..')                       
  In [21]: os.getcwd()                           Out[21]: '/Users'
  In [24]: os.system('touch test.py')            Out[24]: 0
  In [26]: os.stat('test.py')                    Out[26]: os.stat_result(st_mode=33188, st_ino=2252266, st_dev=16777223, st_nlink=1, st_uid=501, st_gid=20, st_size=0, st_atime=1549423963, st_mtime=1549423963, st_ctime=1549423963)
  In [27]: os.rename('test.py','new.py')        
  In [28]: os.remove('new.py')                  
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   | In [29]: import os
  In [30]: os.system('touch test.py')               Out[30]: 0
  In [31]: os.mkdir('test')                        
  In [34]: abspath = os.path.abspath('test.py')    
  In [35]: abspath Out[35]: '/Users/minutesheep/test.py'
 
  In [37]: os.path.split(abspath)                   Out[37]: ('/Users/minutesheep', 'test.py')
  In [38]: os.path.dirname(abspath)                 Out[38]: '/Users/minutesheep'
  In [39]: os.path.basename(abspath)                Out[39]: 'test.py'
  In [40]: os.path.exists(abspath)                  Out[40]: True
  In [41]: os.path.isabs(abspath)                   Out[41]: True
  In [42]: os.path.isfile(abspath)                  Out[42]: True
  In [43]: os.path.isdir('test')                    Out[43]: True
   | 
 
二、sys模块
想要完全了解它,请查看其官方文档:https://docs.python.org/3.5/library/sys.html
这里只记录一些常用方法:
1 2 3 4 5 6 7 8 9 10 11 12
   | In [1]: import sys          
  In [2]: sys.argv             Out[2]: ['/Users/minutesheep/.pyenv/versions/3.5.2/bin/ipython']
  In [3]: sys.version          Out[3]: '3.5.2 (default, Feb  3 2019, 22:37:21) \n[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)]'
  In [4]: sys.path            
  In [5]: sys.platform         Out[5]: 'darwin'
   | 
 
三、项目目录树架构
在写一个项目时,不可能只有一个程序文件,往往含有许多个不同的程序文件,这些程序文件不能都放在同一个文件夹,需要对这些文件进行归类整理。通常一个项目有可执行文件、测试文件、源代码文件、配置文件、日志文件、读我文件,这些文件应当有详细的分类,建议大家整理成如下目录树结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   |  SpeakLanguage                      ├── README.md                      ├── bin                            │   └── speak.py                   ├── conf                           │   ├── conf1.cfg                  │   └── conf2.cfg                  ├── log                            │   └── log.log                    ├── requirements.txt               └── speak_language                     ├── chinese.py                     ├── english.py                     ├── main.py                        └── test                      
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | 
  import os import sys
  BASE_DIR = os.path.dirname(os.path.dirname(     os.path.abspath(__file__)))  
  sys.path.append(BASE_DIR)  
  if __name__ == '__main__':     from speak_language.main import run  
      run()  
 
  | 
 
1 2 3 4
   | 
  def chinese():     print('我是小绵羊')
 
  | 
 
1 2 3 4
   | 
  def english():     print('I am MinuteSheep')
 
  | 
 
1 2 3 4 5 6 7 8 9
   | 
  from speak_language import chinese from speak_language import english
 
  def run():     chinese.chinese()     english.english()
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   | 
  程序说明: xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx -------------------------- 项目环境介绍: xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx -------------------------- 需要的模块: xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx -------------------------- 项目目录树: xxxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxx -------------------------- 作者:MinuteSheep 联系方式:xxxxxx
 
  | 
 
1 2 3 4
   | 
  我是小绵羊 I am MinuteSheep
 
  |