• python os 模块


    os 模块

    可以通过os模块调用系统命令,获得路径,获取操作系统的类型等都是使用该模块

    1、通过os 获取系统类型:os.name()
    import os
    print(os.name)

    nt

    nt 代表windows posix

    linux

    >>> import os
    >>> print(os.name)
    posix
    >>> 
    

    2、执行系统命令:
    os.system('命令') 只会调用系统的命令
    os.popen('命令') os.popen方法,os.popen()返回的是一个file对象

    import os
    print(os.name)
    os.system('dir')
    print('=====================================')
    f = os.popen('ipconfig')
    print(type(f))
    text = f.read()
    print(text)
    print('======================================')
    print(os.popen('ping www.qq.com').read())
    

    运行:

    C:Python27python.exe D:/Python/modules/os_modules.py
    nt
     ������ D �еľ��� ����
     �������� D856-1315
    
     D:Pythonmodules ��Ŀ¼
    
    2017/11/10  17:13    <DIR>          .
    2017/11/10  17:13    <DIR>          ..
    2017/11/10  17:13               379 os_modules.py
    2017/11/10  10:19               496 time_datetime.py
                   2 ���ļ�            875 �ֽ�
                   2 ��Ŀ¼ 207,220,948,992 �����ֽ�
    =====================================
    <type 'file'>
    
    Windows IP ����
    
    
    ��̫�������� vEthernet (DockerNAT):
    
       �����ض��� DNS ��׺ . . . . . . . : 
       �������� IPv6 ��ַ. . . . . . . . : fe80::f8b6:b7f6:62e:cca6%5
       IPv4 ��ַ . . . . . . . . . . . . : 10.0.75.1
       ��������  . . . . . . . . . . . . : 255.255.255.0
       Ĭ������. . . . . . . . . . . . . : 
    
    ��̫�������� vEthernet (Only-NET):
    
       �����ض��� DNS ��׺ . . . . . . . : 
       �������� IPv6 ��ַ. . . . . . . . : fe80::c1b8:d073:8020:296f%20
       IPv4 ��ַ . . . . . . . . . . . . : 11.11.11.10
       ��������  . . . . . . . . . . . . : 255.255.255.0
       Ĭ������. . . . . . . . . . . . . : 
    
    ��̫�������� vEthernet (NAT):
    
       �����ض��� DNS ��׺ . . . . . . . : 
       �������� IPv6 ��ַ. . . . . . . . : fe80::5554:d492:e44f:fedc%10
       IPv4 ��ַ . . . . . . . . . . . . : 172.30.10.103
       ��������  . . . . . . . . . . . . : 255.255.0.0
       Ĭ������. . . . . . . . . . . . . : 172.30.1.1
    
    ��̫�������� ��̫�� 2:
    
       ý��״̬  . . . . . . . . . . . . : ý���ѶϿ�����
       �����ض��� DNS ��׺ . . . . . . . : 
    
    ���߾����������� WLAN:
    
       ý��״̬  . . . . . . . . . . . . : ý���ѶϿ�����
       �����ض��� DNS ��׺ . . . . . . . : 
    
    ��̫�������� ������������:
    
       ý��״̬  . . . . . . . . . . . . : ý���ѶϿ�����
       �����ض��� DNS ��׺ . . . . . . . : 
    
    ======================================
    
    ���� Ping www.qq.com [125.39.240.113] ���� 32 �ֽڵ�����:
    ���� 125.39.240.113 �Ļظ�: �ֽ�=32 ʱ��=13ms TTL=54
    ���� 125.39.240.113 �Ļظ�: �ֽ�=32 ʱ��=12ms TTL=54
    ���� 125.39.240.113 �Ļظ�: �ֽ�=32 ʱ��=7ms TTL=54
    ���� 125.39.240.113 �Ļظ�: �ֽ�=32 ʱ��=14ms TTL=54
    
    125.39.240.113 �� Ping ͳ����Ϣ:
        ���ݰ�: �ѷ��� = 4���ѽ��� = 4����ʧ = 0 (0% ��ʧ)��
    �����г̵Ĺ���ʱ��(�Ժ���Ϊ��λ):
        ��� = 7ms��� = 14ms��ƽ�� = 11ms
    
    
    Process finished with exit code 0
    
    

    3、目录和文件相关操作

    os.getcwd() : 获取路径

    import os
    print(os.getcwd())
    # D:Pythonmodules
    

    os.chdir() : 切换目录

    os.chdir(r'C://')
    print(os.getcwd())
    
    # C:
    

    os.listdir() : 列出目录文件 返回一个列表

    print(os.listdir(os.getcwd()))
    
    # ['os_modules.py', 'time_datetime.py']
    

    os.mkdir() : 创建目录

    os.mkdir('os-test')
    print(os.listdir(os.getcwd()))
    
    # ['os-test', 'os_modules.py', 'os_test', 'time_datetime.py']
    

    os.remove() : 删除目录下的文件

    os.chdir('os-test')
    print(os.listdir(os.getcwd()))
    os.remove('remove')
    print(os.listdir(os.getcwd()))
    
    # ['remove']
    # []
    
    

    os.linesep : 打印操作系统的分隔符

    print(os.linesep)
    

    os.path.join(os.getcwd(), 'aaa', ‘bbb’, ‘ccc’) : 拼接出来多级目录

    print(os.path.join(os.getcwd(), 'aaa', 'bbb', 'ccc'))
    print(os.listdir(os.getcwd()))
    
    # D:Pythonmodulesaaabbccc
    # ['os-test', 'os_modules.py', 'time_datetime.py']
    

    os.path.split('文件或者目录'): 把最后文件和目录分开

    print(os.path.split(r'D:Pythonmodulesaaabbccc'))
    
    # ('D:\Python\modules\aaa\bbb', 'ccc')
    

    os.path.splitext('文件') : 把文件的后缀名和前面分开,返回一个tuple

    print(os.path.splitext('os_modules.py'))
    
    # ('os_modules', '.py')
    

    os.path.splitdrive('目录') : 目录和后面分开

    print(os.path.splitdrive(r'D:/Python/modules/os_modules.py'))
    
    # ('D:', '/Python/modules/os_modules.py')
    
  • 相关阅读:
    java-version
    Centos7使用google-Chrome浏览器
    sign_and_send_pubkey: signing failed: agent refused operation
    删除带中划线的数据库
    yum update报错RPM数据库问题
    Centos的timedatectl
    /var/lib/docker/overlay2/db72dadf047395c178feb0eaedd932b48e283abcaa5a870c0c746bab8ee6f76a: no such file or directory
    /bin/bash^M: 坏的解释器: 没有那个文件或目录
    repeat()函数
    (Problem 47)Distinct primes factors
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7815965.html
Copyright © 2020-2023  润新知