python学习-文件操作

Posted on 2021-01-06 21:22  明和乐儿  阅读(80)  评论(0编辑  收藏  举报
# 打开并创建文件
# my_file = open(r"D:\pythonCode\07day_code\index.html", "w", encoding="utf-8")
# #
# my_file.write("<title>我的文件</title>")
# my_file.close()

# 一行
# content = my_file.readline()
# content = my_file.readline()

# 所有行
# content = my_file.readlines()
# print(content)
# my_file.close()


# 打开并创建文件
# my_file = open(r"C:\Users\Administrator\Desktop\index.html", "w", encoding="utf-8")
# #
# my_file.write("<title>我的文件</title>")
# my_file.close()

# 文件备份
# my_file =open("index.html", "r", encoding="utf-8")
# my_newFile = open("index[备份].html", "w", encoding="utf-8")
# content = my_file.read()
# my_newFile.write(content)
# my_file.close()
# my_newFile.close()

# 大文件怎么备份
# my_file = open(r"C:\Users\Administrator\Desktop\day02\day02\01-视频\02-今日内容介绍.itfeat", "rb")
# my_newFile = open("v[备份].itfeat", "wb")
# while True:
# content = my_file.read(1024)
# my_newFile.write(content)
# if len(content) == 0:
# break
# my_file.close()
# my_newFile.close()

# 文件操作
# import os
# os.rename("index.html", "web_info.html")
# os.rmove("v[备份].itfeat")
# path = os.mkdir(r"C:\Users\Administrator\Desktop\wo")
# os.getcwd()
# os.chdir("../")
# os.listdir(r"C:\Users\Administrator\Desktop")
# os.rmdir(r"C:\Users\Administrator\Desktop\wo")

# path = r"C:\Users\Administrator\Desktop\wo"

# 创建 五个文件夹
# for x in range(1, 6):
# with open(path + "\%d.txt" % x, "w", encoding="utf-8") as my_file:
# my_file.write("2021/1/6 %d" % x)

# 删除 文件
# import os
# os.chdir(path)
# for name in os.listdir():
# os.remove(name)


# 修改文件名
# import os
# os.chdir(path)
# for name in os.listdir():
# os.rename(name, "奇技课堂--"+ name)