python 各级目录文件读取

目录结构

 python 各级目录文件读取_第1张图片

import pytest


def test_01():
    # 同级文件
    with open('1.txt', 'r', encoding='utf-8') as file:
        content = file.read()
        print(content)

def test_02():
    # 同级目录的下的文件
    with open(r'upfile/2.txt', 'r', encoding='utf-8') as file:
        content = file.read()
        print(content)

def test_03():
    # 上一级的文件
    with open(r'../3.txt', 'r', encoding='utf-8') as file:
        content = file.read()
        print(content)

def test_04():
    # 上上一级的文件
    with open(r'../../4.txt', 'r', encoding='utf-8') as file:
        content = file.read()
        print(content)

def test_05():
    # 同级目录下的目录下的文件
    with open(r'upfile/up/5.txt', 'r', encoding='utf-8') as file:
        content = file.read()
        print(content)

if __name__ == '__main__':
    pytest.main('-s -v test_case_wenj_1.py')
    pass

python 各级目录文件读取_第2张图片

 

你可能感兴趣的:(#,python,#,pytest测试框架,pytest,python)