zoukankan      html  css  js  c++  java
  • python3之os、sys

    os模块

    # 显示当前使用平台:"nt":windows;"posix":Linux
    >>> os.name
    'nt'
    
    # 当前工作目录
    >>> os.getcwd()
    'D:\python_file'
    
    # 执行系统操作指令
    >>> os.system("echo 'hello world'")
    
    # 当前工作目录下的文件或目录
    >>> os.listdir()
    ['.idea', 'Atm', 'day5', 'request_study', 'study', 'venv']
    
    # 返回指定目录下的文件和目录名
    >>> os.listdir(r"D:python_fileday5")
    ['os.py', 'randoms.py', 'time_test.py', '__init__.py']
    
    # 显示当前操作系统下的路径分隔符
    >>> os.sep
    '\'
    
    # 显示当前操作系统下的行终止符
    >>> os.linesep
    '
    '
    
    # 用于分割文件路径的字符串
    >>> os.pathsep
    ';'
    
    # 获取系统环境变量
    >>> os.environ
    
    # 返回当前目录
    >>> os.curdir
    '.'
    
    # 返回上级目录
    >>> os.pardir
    '..'
    
    
    
    # 切换目录到指定目录
    >>> os.chdir(r"D:python3.7")
    
    # 创建单层目录
    >>> os.mkdir("a")
    
    # 生成多层递归目录
    >>> os.makedirs(r"D:python_file
    equest_files_001_002_003")
    
    # 删除指定的文件
    >>> os.remove("a.txt")
    
    # 删除指定目录
    >>> os.rmdir(r"D:python_file
    equest_files_001_002_003")
    
    # 删除目录【文件夹为空删除】
    >>> os.removedirs("Dpython_file")
    
    # 列出当前目录下的文件
    >>> os.listdir(".")
    
    # 返回文件或者目录信息
    >>> os.stat("oldname.txt")
    
    
    os.path
    
    # 返回绝对路径
    >>> os.path.abspath(".")
    'D:\python3.7'
    
    # 返回结尾目录、文件名
    >>> os.path.basename(r"D:python_fileday5") 
    'day5'
    
    # 返回list(多个路径)中,所有path共有的最短的路径。
    >>> list=[r"D:a",r"D:a",r"D:ac"]     
    >>> os.path.commonprefix(list)     
    'D:\a'
    
    # 返回文件目录
    >>> os.path.dirname(r"D:aca.txt") 
    'D:\a\b\c'
    >>> os.path.dirname(r"D:ac") 
    'D:\a\b'
    >>> os.path.dirname(r"D:ac")            
    SyntaxError: EOL while scanning string literal
    
    # 路径存在则返回True,路径损坏返回False
    >>> os.path.exists(r"D:ac")        
    False
    
    # 返回最后一次进入此path的时间。
    >>> os.path.getatime(r"D:python_file")        
    1535940322.6286159
    
    # 返回在此path下最后一次修改的时间。
    >>> os.path.getmtime(r"D:python_file")        
    1535940322.6286159
    
    # 判断是否为绝对路径
    >>> os.path.isabs(r"D:ac")      
    True
    
    # 判断路径是否为文件
    >>> os.path.isfile(r"D:ac")      
    False
    
    # 判断路径是否为目录
    >>> os.path.isdir(r"D:ac")      
    False
    
    # 判断路径是否为链接
    >>> os.path.islink(r"D:ac")      
    False
    
    # 判断路径是否为挂载点()
    >>> os.path.ismount(r"D:ac")      
    False
    
    # 把目录和文件名合成一个路径
    >>> os.path.join(r"D:/", r"/a.txt")      
    'D:/a.txt'
    
    # 转换path的大小写和斜杠
    >>> os.path.normcase(r"D:ac")      
    'd:\a\b\c'
    
    # 判断目录或文件是否相同
    >>> os.path.samefile(r"D:ac", r"D:ad")
    False
    
    # 把路径分割成dirname和basename,返回一个元组
    >>> os.path.split(r"D:ad")          
    ('D:\a\b', 'd')

     sys模块

    1 sys.argv           命令行参数List,第一个元素是程序本身路径
    2 sys.exit(n)        退出程序,正常退出时exit(0)
    3 sys.version        获取Python解释程序的版本信息
    4 sys.maxint         最大的Int值
    5 sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    6 sys.platform       返回操作系统平台名称
    7 sys.stdout.write('please:')
    8 val = sys.stdin.readline()[:-1]
    import shutil
    
    # 将文件内容拷贝到另一个文件中,可以部分内容;文件b可以不存在
    # shutil.copyfileobj("a.txt", "b", length=100)
    # f1 = open("a.txt")
    # f2 = open("b", "w")
    # shutil.copyfileobj(f1, f2)
    
    # 拷贝文件,文件b可以不存在
    # shutil.copyfile("a.txt","b")
    
    # 仅拷贝权限,内容、组、用户均不变;文件b必须存在
    shutil.copymode("a.txt","b")
  • 相关阅读:
    sqlserver,获取调用存储过程返回数据的方法。
    手动为弹窗添加一个阴影背景。
    bootstrap资料链接
    进入Linux救援(rescue)模式的四大法门
    virtual box 5.2.12 扩展包安装
    pypi配置国内开源镜像
    vs2015利用python加载dll调试配置
    ubuntu18安装ubuntu kylin软件中心
    firefox快捷键窗口和标签类
    设置双网卡路由
  • 原文地址:https://www.cnblogs.com/gxfaxe/p/9577201.html
Copyright © 2011-2022 走看看