zoukankan      html  css  js  c++  java
  • python, 操作文件和目录

    操作系统提供的命令只是简单地调用了操作系统提供的接口函数,Python内置的os模块也可以直接调用操作系统提供的接口函数

    基本功能

    import os
    #操作系统类型
    os.name
    #posix:Linux、Unix或Mac OS X,nt:Windows系统
    
    #要获取详细的系统信息,Windows上不提供
    os.uname()
    
    #环境变量查看
    os.environ
    #要获取某个环境变量的值,可以调用os.environ.get('key')
    os.environ.get('PATH')
    os.environ.get('x', 'default')

    操作文件和目录

    # 查看当前目录的绝对路径:
    os.path.abspath('.')
    '/Users/michael'
    # 在某个目录下创建一个新目录,首先把新目录的完整路径表示出来:
    os.path.join('/Users/michael', 'testdir')
    '/Users/michael/testdir'
    # 然后创建一个目录:
    os.mkdir('/Users/michael/testdir')
    # 删掉一个目录:
    os.rmdir('/Users/michael/testdir')
    
    #合并os.path.join()
    #拆分路径
    os.path.split('/Users/michael/testdir/file.txt')
    ('/Users/michael/testdir', 'file.txt')
    
    #文件扩展名
    os.path.splitext('/path/to/file.txt')
    ('/path/to/file', '.txt')
    
    #shutil模块中找到很多实用函数,它们可以看做是os模块的补充
    
    #当前目录下的所有目录
    [x for x in os.listdir('.') if os.path.isdir(x)]
    #列出所有的.py文件
    [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']

    参考文章  https://docs.python.org/3/library/os.html?highlight=os#module-os

  • 相关阅读:
    eventkeyboardmouse
    代理 IP
    网关 192.168.2.1 114.114.114.114 dns查询
    http ssl
    SSDP 抓包
    抓包登录信息提交
    危险的input 微博的过去
    firstChild.nodeValue
    浏览器控制台
    haproxy 中的http请求和https请求
  • 原文地址:https://www.cnblogs.com/xiexiaoxiao/p/7082816.html
Copyright © 2011-2022 走看看