文件对象
文件访问模式
with open
读取
with open(r"<文件路径>","r") as f
#文件路径要用引号引起来
#在指定文件路径前要加 r
#第二个r为读操作
#f变量表示操作的文件
with open(r"C:\Users\LEGION\Desktop\test.txt","r") as f :
file=f.readlines() #逐条读取f中的语句并存储在file(列表)中
file2=f.readline() #file2存储的为字符串格式
print(type(file))
print(type(f))
print(file)
print(f)
#输出结果
#<class 'list'>
#<class '_io.TextIOWrapper'>
#['asd\n', 'as\n', 'das\n', 'd\n', 'as\n', 'das\n', 'd']
#<_io.TextIOWrapper name='C:\\Users\\LEGION\\Desktop\\test.txt' mode='r' encoding='cp936'>
写入
创建文件并写入,若有该文件则直接写入
-
写入多行
n = ["a","b","c"] with open(r"C:\Users\LEGION\Desktop\test2.txt","w") as f : for i in n : f.writelines(i+"\n") with open(r"C:\Users\LEGION\Desktop\test2.txt","r") as f : x = f.readlines() print(x) #输出['a\n', 'b\n', 'c\n']
-
写入一行
n = ["a","b","c"] with open(r"C:\Users\LEGION\Desktop\test2.txt","w") as f : for i in n : f.writelines(i) #去掉换行 with open(r"C:\Users\LEGION\Desktop\test2.txt","r") as f : x = f.readlines() print(x) #输出['abc']
open()
-
建立文件对象,读取文件对象
open()
read()括号内不写为读取所有内容
file = open(r"C:\Users\LEGION\Desktop\test.txt","r") ''' 或 with open(file = "C:\Users\LEGION\Desktop\test.txt", mode = "r") as f : print(f.read()) ''' print(file.readlines()) #输出结果 ['asd\n', 'as\n', 'das\n', 'd\n', 'as\n', 'das\n', 'd']
file = open(r"C:\Users\LEGION\Desktop\test.txt","rb") print(file.readlines()) # "rb"中的"b"是以字节形式读取 #输出结果 [b'asd\r\n', b'as\r\n', b'das\r\n', b'd\r\n', b'as\r\n', b'das\r\n', b'd'] #b表示以二进制形式打开 #\r表示回车 #\n表示换行
read()中输入数字
file = open(r"C:\Users\LEGION\Desktop\test.txt","rb") file.read(5) #读取前五个字节 file.read(5) file.read(5) #连续使用则继续向后读取
-
再次读取文件、偏移量seek
file = open(r"C:\Users\LEGION\Desktop\test.txt","r") print(file.read()) #读取一次后便不再读取 #此时可用以下命令继续启用读取 file.seek(0,0) #第一个0表示偏移量,第二个0表示起始位置 #偏移量相当于光标的位置
-
关闭文件,保存
file.close()
写入
-
打开文件
-
写入文件
-
保存关闭文件
-
w
文件不存在则创建,存在则覆盖 -
a
追加,不存在则创建 -
写入
-
f.write()
write() 内建方法的功能是把含有文本数据或二进制数据的字符串写入到文件中去。
写入文件时,不会自动添加结束标志,需要程序员手工输入。
w
覆盖f = open(r"./test.txt","w") f.write("abcdef") f.close() f = open(r"./test.txt","w") f.write("123456") f.close() # 执行第一个 write 无此文件则创建文件并写入 # 执行第二个 write 的内容会覆盖第一次写入的内容 # 最后 test.txt 中的内容为 123456
a
追加f = open(r"./test.txt","a") f.write("abcdef") f.close() f = open(r"./test.txt","a") f.write("123456") f.close() # 执行第一个 write 无此文件则创建文件并写入 # 执行第二个 write 的内容会追加到第一次写入的内容后 # 最后 test.txt 中的内容为 ''' abcdef123456 newStr ''' # write() 写入文件时,不会自动添加结束标志,需要程序员手工输入。
-
f.writelines()
多行写入f = open(r"C:\Users\LEGION\Desktop\test.txt","w") f.writelines(["one\r\n","two\r\n","three\r\n"]) #多行写入 f.close()
以二进制形式写入
f = open(r"C:\Users\LEGION\Desktop\test.txt","wb") f.writelines([b'one\r\n',b'two\r\n',b'three\r\n']) #多行写入 f.close()
‘\r’ 回车,回到当前行的行首,而不会换到下一行,如果接着输出的话,本行以前的内容会被逐一覆盖;
‘\n’ 换行,换到当前位置的下一行,而不会回到行首;
-
-
读取拷贝
f1 = open(r"C:\Users\LEGION\Desktop\test.txt","r") f2 = open(r"C:\Users\LEGION\Desktop\test2.txt","w") data=f1.read() # data 为字符串 str 属性 f2.write(data) f1.close() f2.close()
-
当读取拷贝其他类型如exe文件时需以二进制读取写入
f1 = open(r"C:\Users\LEGION\Desktop\cmd - 管理员.lnk","rb") f2 = open(r"C:\Users\LEGION\Desktop\cmd2.exe","wb") data=f1.read() f2.write(data) f1.close() f2.close()
读取
-
read读取受内存限制,若挤满内存则可能宕机
大文件读取时限制字节数
file.read(4096) #一次只能读4kB
f1 = open(r"C:\Users\LEGION\Desktop\cmd - 管理员.lnk","rb") f2 = open(r"C:\Users\LEGION\Desktop\cmd2.exe","wb") while True: data=f1.read(4096) if data == b"": break #读完数据跳出循环 f2.write(data) f1.close() f2.close()
-
当 .read() 读取完文件时,光标到文件最后的位置,此时再此读取文件则读取不到内容,需要重新打开文件或者将光标移动到最前面
-
TabError: inconsistent use of tabs and spaces in indentation
因为Python区分块级别是通过Space空格和Table制表来进行区分的,并且对这两种分隔符都有相当严厉的规定,要么都用空格键缩进,要么都用tab键缩进,不能混用。
常用函数
- .read() 方法比较适合读取二进制文件,包括 exe 程序,图片等文件,不适合读取纯文本文件
- .readline() 方法读取完一行后光标移动,再次执行.readline() 则读取下一行内容
文件迭代
-
遍历文件中每一行
f=open(r"C:\Users\LEGION\Desktop\asd\tw.txt","r") for i in f: print(i) print(type(i)) ''' 输出效果 badmonkeys <class 'str'> 123456 <class 'str'> password <class 'str'> '''
-
解决空行问题
f=open(r"C:\Users\LEGION\Desktop\asd\tw.txt","r") for i in f: print(i.strip()) print(type(i)) ''' badmonkeys <class 'str'> 123456 <class 'str'> password <class 'str'> 12345678 <class 'str'> root <class 'str'> '''
-
.strip()
用于移除字符串头尾指定的字符或字符序列-
默认为空格或换行符
-
移除指定字符
str = "aaabcdefa" print(str.strip("a")) # 输出 bcdef
-
破解 /etc/shadow 密码脚本
-
shadow 文件存储的密文
root:$6$QhN1tYrOI14JIKNd$rbDhFsEd2F9L.S4fnBTZHcNUb.VPXb8sUIez7e1VMeu8qpm136aFrb.Qt/6nga.BVwEbCwW8mtgNbKHC3DR/90::0:99999:7:::
-
hash 密码字段结构
6
标识使用第6种加密模式,即SHA-512
$6$QhN1tYrOI14JIKNd$
是加密盐值salt
rbDhFsEd2F9L.S4fn……
是密码hash。 -
需要使用到
crypt
模块,此模块在 Windows 上无法使用,需要在 Linux 上使用 -
使用
split.(":")
将密码单独提取出来shadow = "root:$6$QhN1tYrOI14JIKNd$rbDhFsEd2F9L.S4fnBTZHcNUb.VPXb8sUIez7e1VMeu8qpm136aFrb.Qt/6nga.BVwEbCwW8mtgNbKHC3DR/90::0:99999:7:::" password = shadow.split(":") print(password) ''' 输出列表 ['root', '$6$QhN1tYrOI14JIKNd$rbDhFsEd2F9L.S4fnBTZHcNUb.VPXb8sUIez7e1VMeu8qpm136aFrb.Qt/6nga.BVwEbCwW8mtgNbKHC3DR/90', '', '0', '99999', '7', '', '', ''] ''' # 可以使用 password = shadow.split(":")[1] 单独提取密文
-
find
-
查看某个字符在字符串 str 中的位置
str.find("字符")
-
查看某个字符在字符串 str 中的位置(从右往左)
str.rfind("字符")
-
查看第 n 个该字符 str 中的位置
str.find("字符",n)
-
查看最后一个
$
在密文中的位置print(password[1].rfind("$"))
-
-
提取盐值
salt = password[0:password.rfind("$")] # 字符串截取
-
使用盐值加密方法
crypt.crypt("密文",salt)
-
\r
是一个特殊的转义字符,表示回车(Carriage Return)。它的作用是将光标移动到当前行的开头位置,并覆盖该行的内容。当在终端或控制台上输出文本时,使用"\r"可以实现在同一行上不断更新输出内容
import time for i in range(10): print(f"Progress: {i+1}/10", end="\r") time.sleep(1) print("Done!")
在上述示例中,每秒钟输出一个进度,使用"\r"将光标移动到行首,并覆盖之前的输出内容,实现了在同一行上不断更新进度的效果。最终输出的结果是"Done!"。