文件
#文件的简单操作
#1、读取文件
file = open(“a.txt”,mode=“r”)
x=file.read()
print(x)
file.close()
#这里的file 叫做文件的句柄(变量)用来操作文件第一个参数是文件的路径,第二个参数mode=访问文件的类型,r表示读,默认也是r模式
#2、写文件
file = open("a.txt",mode="w")
file.write("asdfghj")
file.close()
#文件权限
#r表示只读,rb是以二进制格式打开一个文件用于只读,r+表示打开一个文件用于读写,rb+表示已二进制格式打开一个文件用于读写
#w表示只写,wb表示以二进制格式打开一个文件只用于写入,w+表示打开一个文件用于读写,wb+表示以二进制格式打开一个文件用于读写
#a表示打开一个文件用于追加,不能读,ab表示以二进制打开一个文件用于追加,a+是打开一个文件用于读写,ab+是以二进制打开一个文件用于追加
文件操作
#读操作
#read() 一次性读取文件的全部内容,括号内可以赋值规定读取的长度
file = open("a.txt")
ret = file.read()
print(ret)
file.close()
#readline() 每次读取一行,并且自带换行功能,每一行末尾会读到\n
file = open("a.txt")
ret = file.readline()
print(ret)
#readlines() 一次性以行的形式读取文件的所有内容并返回一个list,需要去遍历读出来
file = open("a.txt")
ret=file.readlines()
print(ret,type(ret))
#循环读取 file句柄是一个可迭代的对象,因此可以循环读取文件中的内容,每次读一行
file = open("a.txt")
for line in file:
print(line)
#写操作
#write()
file = open("a.txt",mode="w")
file.write("aaaaa")
file.write("werewwe")
file.close()
#writelines file.writelines(seq) 把seq的内容全部写到文件中(多行一次性写入)
file = open("a.txt",mode="w")
lst = ["aa\n","ddfd","werdf"]
file.writelines(lst)
file.close()
#with 使用本方法操作文件,可以不用关闭文件,会自动关闭文件
with open("a.txt") as file :
ret= file.read()
print(ret)
#乱码
#Python读写文件时,默认使用的编码为平台编码,也就是gbk,在pycharm中创建的文件默认使用的是utf-8编码
with open(“a.txt”,mode=“w”,encoding=“utf-8”) as f:#encoding="utf-8"就是规定了编码
f.write(“哈哈哈”)
os模块
重命名文件 os.rename()
import os
os.rename("a.txt","sd.txt")
#删除文件os.remove()
import os
os.remove("sd.txt")#如果是目录的话会报错
创建目录 mkdir() 生成的单层目录
import os
os.mkdir("zzz")
#创建多级目录makedirs()
os.makedirs("xxxxx\\xxxxx\\xxxxx")
#删除目录 rmdir() 删除一层目录
import os
os.rmdir("zzz")
#删除多级目录
os.removedirs("xxxxx\\xxxxx\\xxxxx")
#获取当前所在目录 getcwd()
import os
path = os.getcwd()
print(path)
#获取目录列表os.listdir(path)
import os
lst=os.listdir(os.getcwd())
print(lst)
#切换所在目录 chdir()
import os
print(os.getcwd())
os.chdir(os.getcwd()+"\\aaa")
print(os.getcwd())
#判断文件或文件夹是否存在
import os
b=os.path.exists("5.5learn.py")
c=os.path.exists("aaa")
d=os.path.exists("wwwww.py")
e=os.path.exists("qqqqq")
print(b,c,d,e)#True True False False
#判断是否为文件 isfile()
import os
print(os.path.isfile("aaa"))#False
print(os.path.isfile("5.5learn.py"))#True
#判断是否为目录 isdir()
import os
print(os.path.isdir("aaa"))#True
print(os.path.isdir("5.5learn.py"))#False
#判断是否为绝对路径
import os
print(os.path.isabs("d:\\pywork\\aaa"))#True
print(os.path.isabs("aaa"))#False
#获取绝对路径
import os
abspath = os.path.abspath("aaa")
print(abspath)
#获取路径中的最后部分
import os
path = os.path.basename("d:\\pywork\\aaa")
print(path)#aaa
#获取路径中的路径部分
import os
path=os.path.dirname("d:\\pywork\\aaa")
print(path)#d:\pywork
#自定义mkdirs方法
import os
def mkdirs(path):
file_lst=path.split("/")
for file in file_lst:
if not os.path.exists(file):
os.mkdir(file)
os.chdir(file)
if __name__=="__main__":
path=input("请输入")
mkdirs(path)
#文件搜索
练习递归
‘’’
用户输入文件名以及开始搜索的路径,搜索该文件是否存在,如遇到文件夹,则进入文件夹继续搜索
‘’’
import os
import sys
sys.setrecursionlimit(1000) # set the maximum depth as 1500
def file_find(file_path, file_name):
if os.path.isdir(file_path):
file_list = os.listdir(file_path)
for each in file_list:
temp_dir = file_path + os.sep + each
if os.path.isdir(temp_dir):
temp = file_find(temp_dir, file_name)
if temp == True:
return True
elif os.path.isfile(temp_dir) and each == file_name:
return True
return False
else:
print('{}不是一个目录'.format(file_path))
print(file_find(os.getcwd(), 'b.txt'))
练习递归
#通过给定的文件夹, 列举出这个文件夹当中, 所有的文件,以及文件夹, 子文件夹当中的所有文件
import os
def listFilesToTxt(dir, file):
file_list = os.listdir(dir)
for file_name in file_list:
new_fileName = dir + "/" + file_name
if os.path.isdir(new_fileName):
file.write(new_fileName + "\n")
listFilesToTxt(new_fileName, file)
else:
file.write("\t" + file_name + "\n")
file.write("\n")
f = open(os.getcwd() + "\\list.txt", "a", encoding='utf-8')
listFilesToTxt(os.getcwd() + "\\dir", f)