zoukankan      html  css  js  c++  java
  • python



    # -*- coding:utf-8 -*-

    '''
    @project: jiaxy
    @author: Jimmy
    @file: study_文件目录操作.py
    @ide: PyCharm Community Edition
    @time: 2018-11-13 09:06
    @blog: https://www.cnblogs.com/gotesting/

    '''

    '''
    目录处理:OS
    '''

    import os


    # 1.新建一个目录:不要以数字开头
    os.mkdir('dirtest')
    # os.mkdir(r'F:培训资料python est111')

    # 2.删除一个目录
    os.rmdir('dirtest')

    # 3.建立多级目录:不能跨级去建,确保除最后一级以外的目录级别都存在
    # os.mkdir('dirtest2')
    # os.mkdir('dirtest2/dirtest3')

    # 4.删除包含有子文件夹的目录:不可以跨级删除
    # os.rmdir('dirtest2/dirtest3')
    # os.rmdir('dirtest2')

    # 5.获取当前路径:具体到目录
    path = os.getcwd()
    print(path)

    # 6.获取当前路径:具体到文件
    file = os.path.realpath(__file__) #表示文件本身
    print(file)

    # 7.如何获取a.txt的路径
    # (1)绝对路径 :NO
    # (2)相对路径 :NO
    # (3)getcwd() :YES
    cwd_path = os.getcwd()
    txt_path = cwd_path + '/dirtest2/dirtest3/a.txt'
    print(txt_path)
    # (4)os.path.realpath() :YES
    real_path = os.path.realpath(__file__)
    print(real_path)
    dir_path = os.path.split(real_path)[0]
    print(dir_path)
    txt_path2 = dir_path + '/dirtest2/dirtest3/a.txt'
    print(txt_path2)

    # 8.判断当前的路径到底是目录还是文件 directory ? file ?
    # (1)os.path.isdir() 判断是否为目录
    print(os.path.isdir(cwd_path))
    print(os.path.isdir(txt_path))
    # (2)os.path.isfile() 判断是否为文件
    print(os.path.isfile(cwd_path))
    print(os.path.isfile(txt_path))
    # (3)os.listdir() 列出目录及文件
    print(os.listdir(cwd_path))
    # (4)os.path.dirname() 返回目录名
    print(os.path.dirname(real_path))
    # (5)os.path.basename(__file__) 返回文件名
    print(os.path.basename(__file__))


    # 9.拼接路径 os.path.join
    new_path1 = os.path.join(cwd_path,'test','test1','test2')
    new_path2 = os.path.join(cwd_path,'test3/test4/test5')
    print(new_path1,new_path2)



  • 相关阅读:
    多测师讲解app测试 _app原理图解_高级讲师肖sir
    多测师讲解 app_模拟器的端口号_高级讲师肖sir
    多测师讲解appium _开启注意点_高级讲师肖sir
    多测师讲解app _xpath插件_高级讲师肖sir
    多测师讲app测试 _appium实战(1)_高级讲师肖sir
    多测师讲解app测试 _ADB常用的指令_高级讲师肖sir
    jmeter分布式测试
    常用命令
    Linux安装pycharm
    请求头信息介绍
  • 原文地址:https://www.cnblogs.com/gotesting/p/9950880.html
Copyright © 2011-2022 走看看