程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(2)

Python - OS操作系统API

发布于2019-10-29 18:38     阅读(1662)     评论(0)     点赞(19)     收藏(0)


 注意: os模块操作大小写不敏感, 如需进行大小写操作,调用string API

import os

# 遍历文件

# root 当前根目录 -- str字符串类型
# dirs 当前根目录下的子目录list集合 -- list列表类型
# files 当前根目录下的文件list集合 -- list列表类型
# path 操作目录 -- str字符串类型
# file 当前根目录下的某一文件 -- str字符串类型
# x 当前根目录下的某一文件名的字符 -- str字符串类型
for root, dirs, files in os.walk(path):
    for file in files:
        for x in file:
            pass

# 重命名文件

os.rename(src, dst)

# 获取当前工作目录

os.getcwd()

# 删除文件

os.remove(path)

# 返回绝对路径
os.path.abspath(path)

# 返回文件名
os.path.basename(path)

# 返回文件路径
os.path.dirname(path)

# 如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。
os.path.exists(path)

# 路径存在则返回True,路径损坏也返回True
os.path.lexists(path)

# 返回最近访问时间(浮点型秒数)
os.path.getatime(path)

# 返回最近文件修改时间
os.path.getmtime(path)

# 返回文件 path 创建时间
os.path.getctime(path)

# 返回文件大小,如果文件不存在就返回错误
os.path.getsize(path)

# 判断是否为绝对路径
os.path.isabs(path)

# 判断路径是否为文件
os.path.isfile(path)

# 判断路径是否为目录
os.path.isdir(path)

# 判断路径是否为链接
os.path.islink(path)

# 判断路径是否为挂载点
os.path.ismount(path)

# 把目录和文件名合成一个路径
os.path.join(path1[, path2[, ...]])

# 转换path的大小写和斜杠
os.path.normcase(path)

# 规范path字符串形式
os.path.normpath(path)

# 从start开始计算相对路径
os.path.relpath(path[, start])

# 判断目录或文件是否相同
os.path.samefile(path1, path2)

# 判断fp1和fp2是否指向同一文件
os.path.sameopenfile(fp1, fp2)

# 把路径分割成 dirname 和 basename,返回一个元组
os.path.split(path)

# 分割路径,返回路径名和文件扩展名的元组
os.path.splitext(path)

# 把路径分割为加载点与文件
os.path.splitunc(path)

# 分割路径,返回路径名和文件扩展名的元组
os.path.splitext(path)

# 路径分隔符
os.path.sep     # \

# 开启外部应用
os.startfile(r"D:\Program Files (x86)\Youdao\YoudaoNote\YoudaoNote.exe")

# 运行外部exe文件
    # 改变工作目录
    os.chdir("ResourcesStation_Ob.encrypt_derios5_1908061814")
    path_01 = r"HUNTER.exe"
    os.system(path_01)

# 暂停运行,按任意键继续
os.system("pause")

# 运行外部bat文件(注:依然在此窗口)
os.system("localHttp.bat")

 

 

 

 

 



所属网站分类: 技术文章 > 博客

作者:放羊人

链接:https://www.pythonheidong.com/blog/article/147819/b44a80b1de830f41f2a9/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)