python-文件操作-os模块

import os

# 本文件演示os模块的基本用法

if __name__ == "__main__":

# 路径操作类函数

    # os.path.join([]):通过字符串列表生成路径。返回字符串
    myPath = os.path.join(r"E:\PyhtonTest\Test")    # myPath='E:\PyhtonTest\Test'
    myPath = os.path.join(r"E:\PyhtonTest\Test","doc")    # myPath='E:\PyhtonTest\Test\doc'
    myPath = os.path.join("E:","PyhtonTest","Test", "doc","mydoc")   # myPath='E:\PyhtonTest\Test\doc\mydoc'

    # 获取当前路径。返回字符串
    myPath = os.getcwd()

    # 将当前路径修改为参数指向的位置,目录必须存在,否则引发FileNotFoundError异常
    os.chdir(r'E:\Class')
    myPath = os.getcwd()  # myPath = 'E:\Class'

    # 新建新目录。目录已存在则产生FileExistsError异常
    try:
        os.makedirs(r'E:\PyhtonTest\Test\doc')  #将在'E:\PyhtonTest\Test'目录下新建一个doc目录
    except FileExistsError:
        pass

    # 将相对路径path转换为绝对路径。返回字符串
    path = r'.'
    myPath = os.path.abspath(path)  # myPath = 'E:\Class'

    # 返回从start路径起,到path路径为止的相对路径。返回字符串
    start=r'e:\TankWar\env'
    path=r'E:\PyhtonTest\Test\doc'
    myPath = os.path.relpath(path, start) # myPath = '..\..\PyhtonTest\Test\doc'

    # 返回path路径中除最后一级外的其他部分。返回字符串
    path = r'E:\PyhtonTest\Test\doc'
    myPath = os.path.dirname(path)   # myPath = 'E:\PyhtonTest\Test'

    # 返回path中最后一级目录名或文件名。返回字符串
    path = r'E:\PyhtonTest\Test\doc'
    myPath = os.path.basename(path) # myPath = 'doc'

    # 返回由path拆分成的,dirname和basename组成的元组。
    path = r'E:\PyhtonTest\Test\doc'
    myPath = os.path.split(path)    # myPath = ('E:\\PyhtonTest\\Test', 'doc')
    myPath = (os.path.dirname(path),os.path.basename(path)) # 效果同上

    # os.path.sep属性能将路径字符串拆分
    list = path.split(os.path.sep)   # list = ['E:', 'PyhtonTest', 'Test', 'doc']

    # 获取path所在文件字节数。返回整数
    path = r'E:\PyhtonTest\Test\osTest.py'
    size = os.path.getsize(path)    # size = 2149

    # 列出path目录下所有文件或文件夹名的字符串列表
    path = r'E:\PyhtonTest\Test'
    list = os.listdir(path) # list = ['.idea', 'doc', 'osTest.py', 'venv']

# 路径判断有关函数

    # 判断path是否为绝对路径,如果是,返回True,否则返回False
    path = r'c:'
    os.path.isabs(path) # 打印False

    # 判断当前path路径是否存在。返回True或False
    path = r'E:\PyhtonTest\Test\doc'
    os.path.exists(path)

    # 判断当前路径是否文件。返回True或False
    os.path.isfile(path)

    # 判断当前路径是否文件夹。返回True或False
    os.path.isdir(path)

# 文件操作类函数

    # 打开文件。以字符串mode为模式,打开字符串path路径的文件。返回File对象
    path = r'E:\PyhtonTest\Test\test.txt'
    mode = "r"  # "r":读模式,只能读文件,不能写文件,文件不存在会报异常
                # "w":写模式,文件内的内容被清空,若文件不存在会新建文件
                # "a":追加模式,文件内内容不清空,仅在文件尾添加新内容,若文件不存在会新建文件
                # "b":二进制模式;与以上模式合用,操作二进制文件。否则操作文本文件
    myFile = open(path,mode)

    # 读取文件。从myFile对象指向的文件中读取n个字符,n不写时取出文件所有内容。返回字符串
    myStr = myFile.read(2)  # myStr = 'fd'
    myStr = myFile.read()   # myStr = 'fdsafdsa\nfdasfsaf'

    # 行模式读取文件。从myFile指向的文件中,以换行符为分隔符,返回每行文本的字符串列表。
    myStr = myFile.readlines()  # myStr = ['fdsafdsa\n', 'fdasfsaf']

    # 逐行读取文件。从myFile指向的文件中,以换行符为分隔符,读入一行。每执行一次,就读一行
    myStr = myFile.readline()  # 读入第一行
    myStr = myFile.readline()  # 读入第二行

    # 写文件。将字符串someStr写入myFile指向的文件中,原内容被删除
    myFile = open(path,"w")
    someStr = "Hello World\nThis is a test file\n"   # write函数需要自己加换行符
    myFile.write(someStr)

    # 关闭文件。关闭myFile指向的文件
    myFile.close()

    print(list)
    print(myStr)
    pass
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值