python os库安装_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")

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值