python作业2 创建删除文件夹写入内容

这段代码演示了如何使用Python的os和shutil模块进行文件和目录的操作,包括获取当前路径、创建和删除目录、在指定目录下创建并写入文本文件、读取文件内容、删除文件等步骤。主要涉及文件路径处理、文件读写及目录管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用:os模块,ospase模块
打印当前路径
在当前路径创建tist目录
在test目录下创建test.txt文档
编test.tst文档,插入hello word
删除test。tst文档
删除test目录

# 引入模块
import os
import shutil


def opentxt(testFile):
    with open(testFile, "w") as f:
        f.write("hello word!")
        print('.txt为空,已写入helloword!')


def closetxt(testFile):
    open(testFile, 'w').close()
    print('.txt错误内容已清空')


def gettxt(testFile):
    with open(testFile, "r") as f:  # 打开文件
        text = f.read()  # 读取文件
    return text


def mkdir(path, txtPath):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    # path = path.rstrip("\\")
    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)
    print('打印当前路径:' + path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        os.makedirs(path)  # 创建目录操作函数
        print('test文件不存在,test文件已创建成功')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print('test文件已存在')
        # .txt文档路径
        testFile = path + '\\' + txtPath
        # 判断txt文件是否存在
        if os.access(testFile, os.F_OK):
            print('.txt已存在')
            # 读取txt文件内容
            text = gettxt(testFile)
            # txt内容为空
            if not text:
                # 写入hello word
                opentxt(testFile)
            #  txt内容不为空
            else:
                if text == 'hello word!':
                    print('.txt内容为:' + text)
                # 如果txt内容不是hello word!清空内容,写入hello word!
                else:
                    print('.txt文本内容错误')
                    # 清空文本内容
                    closetxt(testFile)
                    # 写入hello word
                    opentxt(testFile)

        else:
            print('.txt不存在')
            # 创建txt文件
            file = open(testFile, 'w')
            file.close()
            print('.txt已创建')


# with
# path文件路基
# testFile文本路径
def removetxt(testFile):
    os.remove(testFile)
    print('.txt文件已删除')


def deletetxt(path, testFile):
    # .txt存在
    if os.access(testFile, os.F_OK):
        print('.txt存在')
        # 读取txt文件内容
        text = gettxt(testFile)
        # txt内容为空
        if not text:
            print('.txt内容为空')
        else:
            # 清空txt内容
            closetxt(testFile)
            # 删除test.txt文档
            removetxt(testFile)

    else:
        print('.txt不存在')

    # 删除test文件
    if os.path.exists(path):  # 如果文件存在
        if (os.path.exists(path) == True):
            shutil.rmtree(path)
            print('test文件已删除')
    else:
        print('test文件不存在')


if __name__ == '__main__':
    # 定义要创建的目录
    mkpath = r"C:\\Users\\86183\\Desktop\\项目\\周二作业\\test"
    # 定义要创建的txt文档
    txtPath = 'text' + '.txt'
    # 创建文件夹
    mkdir(mkpath, txtPath)
    # 删除
    # testFile = mkpath + '\\' + txtPath
    # deletetxt(mkpath, testFile)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值