python os庫的使用方法 + 自動化安裝第三方庫腳本


一、os庫基本介紹

os庫提供通用的、基本的操作系統交互功能,包括windows、Mac os、linux

os庫是python標准庫,包含幾百個函數

常用路徑操作、進程管理、環境參數等幾類

  • 路徑操作:os.path子庫,處理文件路徑及信息
  • 進程管理:啟動系統中其他程序
  • 環境參數:獲得系統軟硬件等環境參數

1、路徑操作

os.path子庫以path為入口,用於操作和處理文件路徑

函數 描述
os.path.abspath(path) 返回path在當前系統中的絕對路徑os.path.abspath('test.txt')
輸出:'D:\python\test.txt'
os.path.normpath(path) 歸一化path的表示形式,統一用\分割路徑>>> os.path.normpath(r'D:\testFile\test.txt')
輸出 'D:\testFile\test.txt'
os.path.normpath('D://testFile//test.txt')
輸出'D:\testFile\test.txt'
os.path.relpath(path) 返回當前程序與文件之間的相對路徑(relative path)os.path.relpath(r'D:\testFile\test.txt')
輸出 '..\testFile\test.txt
os.path.dirname(path) 返回path中的目錄名稱os.path.dirname(r'F:\testFile\test.txt')
'F:\testFile'
os.path.basename(path) 返回path中最后的文件名稱,如果最后一級也是目錄,則返回目錄名稱os.path.basename(r'D:\testFile\test.txt')
'test.txt'
os.path.basename(r'D:\testFile')
'testFile'
os.path.split(path) 返回path中目錄和文件名稱,如果path最后一集也是目錄,則都返回目錄名稱如果path只有根路徑,返回根路徑和空路徑os.path.split(r'D:\testFile\test.txt')
('D:\testFile', 'test.txt')
os.path.split(r'D:\testFile')
('D:\', 'testFile')
os.path.split(r'D:')
('D:', '')
os.path.join(path, *paths) 組合path和paths,返回一個路徑字符串
os.path.join(r'D:', '\testFile\test.txt')
'D:\testFile\test.txt'
os.path.exists(path) 判斷path對應文件或目錄是否存在,返回True或False
os.path.exists('test.txt') True
os.path.isfile(path) 判斷path所對應是否為已存在的文件,返回True或False
os.path.isfile('D:\\testFile\\test.txt') True
os.path.isdir(path) 判斷path所對應是否為已存在的目錄,返回True或False
os.path.isdir('D:\\testFile') True
os.path.getmtime(path) 返回path對應文件或目錄最近一次的修改時間
os.path.getmtime('D:\\testFile\\test.txt') 1536545024.503566
os.path.getatime(path) 返回path對應文件或目錄上一次的訪問時間
os.path.getatime('D:\\testFile\\test.txt') 1536033956.367
os.path.getctime(path) 返回path對應文件或目錄創建時間。windows系統中,上一次訪問時間等於創建時間
os.path.getctime('D:\\testFile\\test.txt')
1536033956.367
os.path.getsize(path) 返回path對應文件的大小,以字節為單位os.path.getsize('D:\\testFile\\test.txt') 29

2、進程管理

os.system(command)

  • 執行程序或命令command
  • 在windows系統中,返回值為cmd的調用返回信息

如下例,調用計算器返回值為0

import os
os.system(r'C:\Windows\system32\calc.exe')
返回的是 0 

3、環境參數

函數 描述
os.name 返回正在使用的工作平台,比如windows返回‘nt’,linux/Unix返回‘ posix‘
os.getenv('PATH') 返回系統環境變量os.getenv('PATH')
'C:\Program Files\Java\jdk1.8.0_162\bin;
os.listdir(path) 返回path目錄下所有文件 os.listdir(r'D:\testFile')
['dumps.txt', 'test.txt', 'testTxt.txt']
os.chdir(path) 修改當前程序操作的路徑os.chdir('D:\testFile')
os.getcwd() 返回程序的當前路徑os.getcwd() 輸出 'D:\testFile'
os.getlogin() 獲取當前系統的登錄用戶名稱os.getlogin() 'showgea'
os.cpu_count() 獲得當前系統的CPU數量os.cpu_count() 4
os.urandom(n) 獲得n個字節長的隨機字符串,通常用於加解密運算os.urandom(10)
b'F\x18l\x98\x1c\xfch&\xef\xa6'

二、第三方庫安裝腳本

#BatchInstall.py
import os
libs = {"numpy","matplotlib","pillow","sklearn","requests",\
        "jieba","beautifulsoup4","wheel","networkx","sympy",\
        "pyinstaller","django","flask","werobot","pyqt5",\
        "pandas","pyopengl","pypdf2","docopt","pygame"}
try:
    for lib in libs:
        os.system("pip3 install "+lib)
    print("Successful")        
except:
    print("Failed Somehow")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM