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'

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二、第三方庫安裝腳本

# PipInstall.py
import os

libs = {'numpy','sklearn','pillow','beautifulsoup4','wheel','networkx',\
        'sympy','django','pypdf2','pygame'}

try:
    for lib in libs:
        os.system('pip list ' + lib)
        print('Successful')
except:
    print('Failed')

運行結果:

======================= RESTART: D:/python/PipInstall.py =======================
Successful
Successful
Successful
Successful
Successful
Successful
Successful
Successful
Successful
Successful


免責聲明!

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



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