1. 程式人生 > >os模塊

os模塊

5.0 orm lin help gcc projects php 註意 odin

os,語義為操作系統,所以肯定就是操作系統相關的功能了,可以處理文件和目錄這些我們日常手動需要做的操作,就比如說:顯示當前目錄下所有文件/刪除某個文件/獲取文件大小……

  另外,os模塊不受平臺限制,也就是說:當我們要在linux中顯示當前命令時就要用到pwd命令,而Windows中cmd命令行下就要用到這個,額...我擦,我還真不知道,(甭管怎麽著,肯定不是pwd),這時候我們使用python中os模塊的os.path.abspath(name)功能,甭管是linux或者Windows都可以獲取當前的絕對路徑。

os模塊的常用功能:

1 os.name #顯示當前使用的平臺

技術分享技術分享os.name
‘nt‘ #這表示Windows
>>> os.name ‘posix‘ #這表示Linux 2 os.getcwd() #顯示當前python腳本工作路徑 os.getcwd()
‘C:\\Users\\Capital-D\\PycharmProjects\\untitled‘ #使用pycharm

>>> os.getcwd() ‘/root‘ #Linux平臺在/root目錄直接使用python3命令 3 os.listdir(‘dirname‘) #返回指定目錄下的所有文件和目錄名 相對於os.getcwd路徑下的文件
>>> os.listdir() [‘.idea‘, ‘test‘] >>> os.listdir() [‘.bash_logout‘, ‘Python-3.4.4‘, ‘.mysql_history‘, ‘.tcshrc‘, ‘Python-3.4.4.tar.xz‘, ‘.bash_profile‘, ‘.lesshst‘, ‘install.log.syslog‘, ‘.cshrc‘, ‘04.sql‘, ‘anaconda-ks.cfg‘, ‘test‘, ‘.viminfo‘, ‘phpMyAdmin-4.4.15-all-languages.tar.bz2‘, ‘1test‘, ‘.bashrc‘, ‘binlog.sql‘, ‘back.sql‘, ‘install.log‘, ‘binlog4.sql‘, ‘.bash_history‘, ‘backup.sql‘, ‘text.py‘, ‘.rnd‘, ‘test1‘] 4 os.remove(‘filename‘) #刪除一個文件 [[email protected]
/* */ ~]# touch hahaha [[email protected] ~]# ls
04.sql back.sql binlog.sql install.log.syslog Python-3.4.4.tar.xz text.py 1test backup.sql hahaha phpMyAdmin-4.4.15-all-languages.tar.bz2 test anaconda-ks.cfg binlog4.sql install.log Python-3.4.4 test1 #hahaha(粉色字體)存在
[[email protected]
/* */ ~]# python3
Python 3.4.4 (default, Apr 5 2016, 04:23:19) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.remove(‘hahaha‘) >>> exit() [[email protected] ~]# ls
04.sql anaconda-ks.cfg backup.sql binlog.sql install.log.syslog Python-3.4.4 test text.py 1test back.sql binlog4.sql install.log phpMyAdmin-4.4.15-all-languages.tar.bz2 Python-3.4.4.tar.xz test1 #hahaha已被刪 5 os.makedirs(‘dirname/dirname‘) #可生成多層遞規目錄 [[email protected] ~]# ls
04.sql anaconda-ks.cfg backup.sql binlog.sql install.log.syslog Python-3.4.4 test text.py 1test back.sql binlog4.sql install.log phpMyAdmin-4.4.15-all-languages.tar.bz2 Python-3.4.4.tar.xz test1 [[email protected] ~]# python3
Python 3.4.4 (default, Apr 5 2016, 04:23:19) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.makedirs(‘hahaha/linghuchong‘) >>> exit() [[email protected] ~]# ls
04.sql back.sql binlog.sql install.log.syslog Python-3.4.4.tar.xz text.py 1test backup.sql hahaha phpMyAdmin-4.4.15-all-languages.tar.bz2 test anaconda-ks.cfg binlog4.sql install.log Python-3.4.4 test1 [[email protected] ~]# ls hahaha/
linghuchong [[email protected] ~]# ls hahaha/linghuchong/
[[email protected] ~]#

6 os.rmdir(‘dirname‘) #刪除單級目錄

[[email protected] ~]# ls
04.sql back.sql binlog.sql install.log.syslog Python-3.4.4.tar.xz text.py 1test backup.sql hahaha phpMyAdmin-4.4.15-all-languages.tar.bz2 test anaconda-ks.cfg binlog4.sql install.log Python-3.4.4 test1 [[email protected] ~]# ls hahaha/
linghuchong [[email protected] ~]# ls hahaha/linghuchong/
[[email protected] ~]# python3
Python 3.4.4 (default, Apr 5 2016, 04:23:19) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.rmdir(‘hahaha/linghuchong‘) >>> exit() [[email protected] ~]# ls hahaha/
[[email protected] ~]# 7 os.rename("oldname","newname") #重命名文件 os.getcwd() ‘/root/hahaha‘
>>> os.listdir() [‘test‘] >>> os.rename(‘test‘,‘test_new‘) >>> os.listdir() [‘test_new‘]

8 os.system() #運行shell命令,註意:這裏是打開一個新的shell,運行命令,當命令結束後,關閉shell

os.system(‘pwd‘) /root/hahaha 0 9 os.sep #顯示當前平臺下路徑分隔符 os.sep
‘/‘ #linux

>>> os.sep ‘\\‘ #windows 10 os.linesep #給出當前平臺使用的行終止符 os.linesep
‘\n‘ #linux

>>> os.linesep ‘\r\n‘ #windows 11 os.environ #獲取系統環境變量
os.environ
environ({‘USERPROFILE‘: ‘C:\\Users\\Capital-D‘, ‘PROCESSOR_ARCHITECTURE‘: ‘x86‘, ‘SESSIONNAME‘: ‘Console‘, ‘UGII_BASE_DIR‘: ‘D:\\Program Files (x86)\\Siemens\\NX 8.0‘, ‘COMMONPROGRAMW6432‘: ‘C:\\Program Files\\Common Files‘, ‘COMMONPROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘MOZ_PLUGIN_PATH‘: ‘C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\plugins\\‘, ‘ALLUSERSPROFILE‘: ‘C:\\ProgramData‘, ‘PYTHONIOENCODING‘: ‘UTF-8‘, ‘NUMBER_OF_PROCESSORS‘: ‘4‘, ‘APPDATA‘: ‘C:\\Users\\Capital-D\\AppData\\Roaming‘, ‘TERM‘: ‘emacs‘, ‘TEMP‘: ‘C:\\Users\\CAPITA~1\\AppData\\Local\\Temp‘, ‘PROGRAMDATA‘: ‘C:\\ProgramData‘, ‘COMSPEC‘: ‘C:\\windows\\system32\\cmd.exe‘, ‘WINDIR‘: ‘C:\\windows‘, ‘PROCESSOR_IDENTIFIER‘: ‘Intel64 Family 6 Model 58 Stepping 9, GenuineIntel‘, ‘PATHEXT‘: ‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY‘, ‘PATH‘: ‘C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Common Files\\NetSarang;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT‘, ‘UGII_ROOT_DIR‘: ‘D:\\Program Files (x86)\\Siemens\\NX 8.0\\UGII\\‘, ‘COMPUTERNAME‘: ‘IDEA-PC‘, ‘USERDOMAIN‘: ‘idea-PC‘, ‘TMP‘: ‘C:\\Users\\CAPITA~1\\AppData\\Local\\Temp‘, ‘SYSTEMROOT‘: ‘C:\\windows‘, ‘PROCESSOR_REVISION‘: ‘3a09‘, ‘FP_NO_HOST_CHECK‘: ‘NO‘, ‘PROGRAMFILES‘: ‘C:\\Program Files (x86)‘, ‘PYTHONDONTWRITEBYTECODE‘: ‘1‘, ‘LOCALAPPDATA‘: ‘C:\\Users\\Capital-D\\AppData\\Local‘, ‘PYTHONUNBUFFERED‘: ‘1‘, ‘LOGONSERVER‘: ‘\\\\IDEA-PC‘, ‘UGII_LANG‘: ‘simpl_chinese‘, ‘SYSTEMDRIVE‘: ‘C:‘, ‘PUBLIC‘: ‘C:\\Users\\Public‘, ‘HOMEPATH‘: ‘\\Users\\Capital-D‘, ‘PYTHONPATH‘: ‘C:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.2\\helpers\\pydev‘, ‘USERNAME‘: ‘Capital-D‘, ‘UGS_LICENSE_SERVER‘: [email protected], ‘USERDOMAIN_ROAMINGPROFILE‘: ‘idea-PC‘, ‘PYCHARM_HOSTED‘: ‘1‘, ‘OS‘: ‘Windows_NT‘, ‘PROCESSOR_ARCHITEW6432‘: ‘AMD64‘, ‘PROGRAMFILES(X86)‘: ‘C:\\Program Files (x86)‘, ‘PROGRAMW6432‘: ‘C:\\Program Files‘, ‘PSMODULEPATH‘: ‘C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\‘, ‘COMMONPROGRAMFILES‘: ‘C:\\Program Files (x86)\\Common Files‘, ‘IPYTHONENABLE‘: ‘True‘, ‘HOMEDRIVE‘: ‘C:‘, ‘CONFIGSETROOT‘: ‘C:\\windows\\ConfigSetRoot‘, ‘PROCESSOR_LEVEL‘: ‘6‘})
技術分享 12 os.path.abspath(path) #顯示當前絕對路徑 os.path.abspath(‘test‘) ‘C:\\Users\\Capital-D\\PycharmProjects\\untitled\\test‘ 13 os.path.dirname(path) #返回該路徑的父目錄 os.path.abspath(‘test‘) ‘C:\\Users\\Capital-D\\PycharmProjects\\untitled\\test‘
>>> os.path.dirname(os.path.abspath(‘test‘)) ‘C:\\Users\\Capital-D\\PycharmProjects\\untitled‘ 14 os.path.basename(path) #返回該路徑的最後一個目錄或者文件,如果path以/或\結尾,那麽就會返回空值。 >>> os.path.dirname(os.path.abspath(‘test‘)) ‘C:\\Users\\Capital-D\\PycharmProjects\\untitled‘
>>> os.path.basename(os.path.dirname(os.path.abspath(‘test‘))) ‘untitled‘ 15 os.path.isfile(path) #如果path是一個文件,則返回True [[email protected] ~]# ls
04.sql back.sql binlog.sql install.log.syslog Python-3.4.4.tar.xz text.py 1test backup.sql hahaha phpMyAdmin-4.4.15-all-languages.tar.bz2 test anaconda-ks.cfg binlog4.sql install.log Python-3.4.4 test1 [[email protected] ~]# python3
Python 3.4.4 (default, Apr 5 2016, 04:23:19) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.path.isfile(‘test‘) True 16 os.path.isdir(path) #如果path是一個目錄,則返回True os.path.isdir(‘hahaha‘) True 17 os.stat() #獲取文件或者目錄信息 os.stat(‘test‘) os.stat_result(st_mode=33188, st_ino=137149, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=85, st_atime=1462373193, st_mtime=1462373186, st_ctime=1462373186) 18 os.path.split(path) #將path分割成路徑名和文件名。(事實上,如果你完全使用目錄,它也會將最後一個目錄作為文件名而分離,同時它不會判斷文件或目錄是否存在) os.path.split(‘/root/test‘) (‘/root‘, ‘test‘) 19 os.path.join(path,name) #連接目錄與文件名或目錄 結果為path/name os.path.join(‘/root/haha‘,‘test‘) ‘/root/haha/test‘
>>> os.name
‘nt‘                  #這表示Windows
>>> os.name
‘posix‘             #這表示Linux

os模塊