zoukankan      html  css  js  c++  java
  • 文件基础操作及os模块

    1. o s 模块
    import os
    os.listdir(path):返回该路径下的所有文件和文件夹,第一层。
    a = os.listdir("C:/Users/Administrator/Desktop/os模块测试文件夹")
    os.path.isfile(path):判断给定的路径不是一个文件,如果是则返回True,如果不是则返回False
    b = os.path.isfile("C:/Users/Administrator/Desktop/os模块测试文件夹/新建文件夹1/1.1/1.1.1")
    os.path.isdir(path):判断给定的路径是不是文件夹。
    # c = os.path.isdir("C:/Users/Administrator/Desktop/os模块测试文件夹/")
    os.path.exists(path):判断文件或者目录是否存在,存在则返回True,否则返回False
    # d = os.path.isdir("C:/Users/Administrator/Desktop/os模块测试文件夹/")
    os.mkdir(path):创建文件夹,但是文件夹的名字不能有特殊符号(*,?,)
    # try:
    # os.mkdir("C:/Users/Administrator/Desktop/os模块测试文件夹/number")
    # except:
    # print("文件夹创建失败")
    # os.makedirs(dirname):可以生成多层递归目录,如果目录全部存在,则创建目录失败。
    # os.makedirs("name1")
    os.path.rmdir(path):删除给定文件夹。
    # os.rmdir("C:/Users/Administrator/Desktop/os模块测试文件夹/number")
    os.remove(path):删除文件的
    # os.remove("C:/Users/Administrator/Desktop/os模块测试文件夹/新建文本文档3.txt")
    os.removedirs(dirname) 可以删除多层递归的空目录,若目录中有文件则无法删除。
    os.path.join(path,name):路径的拼接
    # e = os.path.join("C:/Users/Administrator/Desktop/os模块测试文件夹", "11111")
    # print(e)
    os.path.dirname:返回文件的目录,如果给定的是目录,则直接返回。(也就是返回当前文件的文件夹)
    # f = os.path.dirname("C:/Users/Administrator/Desktop/os模块测试文件夹")
    os.chdir():改变当前目录到指定目录。
    # os.chdir("C:/Users/Administrator/Desktop")
    os.rename():重命名目录名或者文件名。重命名后的文件名已存在,则重命名失败。
    # os.rename("C:/Users/Administrator/Desktop/os模块测试文件夹/新建文件夹1", "巴拉巴拉")



    2.python文件操作
    1. 只读(r, rb)
    # j = open("新建文本文档2.txt", "r", encoding="utf-8")
    # content = j.read()
    # print(content)
    # j.close()
    2. 只写(w, wb)
    # h = open("新建文本文档2.txt", "w", encoding="utf-8")
    # h.write("hello word ,hello python")
    # h.flush()
    # h.close()
    3. 追加(a, ab)
    # i = open("新建文本文档2.txt", mode="a", encoding="utf-8")
    # i.write(" hello"*5)
    # i.flush()
    # i.close()
    4. r+读写
    # j = open("新建文本文档2.txt", mode="r+", encoding="utf-8")
    # content = j.read()
    # j.write(" python"*5)
    # j.flush()
    # print(content)
    # j.close()
    5. w+写读
    # k = open("新建文本文档2.txt", mode="w+", encoding="utf-8")
    # k.write("哈哈")
    # content = k.read()
    # print(content)
    # k.flush()
    # k.close()
    6. a+写读(追加写读)
    # l = open("新建文本文档2.txt", mode="a+", encoding="utf-8")
    # l.write(" hello python")
    # content = l.read()
    # print(content)
    # l.flush()
    # l.close()
    7. 其他操作方法
    # seek(n) 光标移动到n位置, 注意, 移动的单位是byte. 所以如果是UTF-8的中文部分要
    # 是3的倍数.
    # 移动到开头: seek(0)
    # 移动到结尾: seek(0,2) seek的第二个参数表示的是从哪个位置进行偏移, 默认是0, 表
    # 示开头, 1表示当前位置, 2表示结尾

    # tell() 使用tell()可以帮我们获取到当前光标在什么位置


    # tell() 使用tell()可以帮我们获取到当前光标在什么位置
  • 相关阅读:
    文本框字数减少
    区分兼容IE6/IE7/IE8/IE9/FF的CSS HACK写法
    将浏览器兼容代码标明信息并相互分开
    JavaScript正则表达式
    CSS3 @font-face
    iOS 获取手机的使用存储信息
    升级 ox 10.11的系统以后执行 pod install 的时候报错
    Xcode7安装模拟器
    iOS-视图生命周期
    Xcode升级插件失效修复快捷方式
  • 原文地址:https://www.cnblogs.com/zjldeboke/p/13846045.html
Copyright © 2011-2022 走看看