Lilbey
发表于 2023-5-10 16:03:07
{:5_111:}
wakin
发表于 2023-5-10 21:03:36
11
dtwc01
发表于 2023-5-11 12:49:24
lclcluochang
发表于 2023-5-11 16:48:58
0:
如果是读取r或者更新的话,会报错;如果是写入w的话就不会报错,会新建一个文件
1:
flush(),但有时候不可靠
2:
代表这一行就读三个字符
3:
一行,因为writelines只是可以写入多个字符串,但没说要给这些字符串自动换行,换行要自己加换行符
4:
因为在文件中每行末尾都有一个换行符,print语句在打印时也会加上一个换行符,可以使用strip()或者print(end = '')来去除。
5:
是转义字符造成的,在windows当中读取文件路径可以用\,但在python当中,有转义含义。
解决方式:1、在字符串前加r;2、用\\反转义;3、替换成正斜杠。
6:
它会覆盖原来的文件,所以一般不要轻易使用
7:
可以,会新建一个文件
8:
记录文件读取到哪个位置
9:
文件的末尾end of file
10:
f.tell()
0:
f = open('D:\AI\python\课后作业\永久存储Ⅰ\FishC.txt','r')
f.seek(9)
g = f.read(6)
p = open('D:\AI\python\课后作业\永久存储Ⅰ\FishD.txt','w')
p.write(g)
p.close()
f.close()
1:
f = open('D:\AI\python\课后作业\永久存储Ⅰ\FishC.txt','r+')
f.truncate(15)
15
f.close()
2:
f = open(__file__,'r')
for i in f:
print(i,end = '')
3:
fishC121381
发表于 2023-5-11 21:55:22
0.不会,会创建新的
1.有的,f.flush()
2.读取三个字符
3.一行
4.不知道
5.转义字符
6.截断
7.可以
8.标识指向文件的位置
9.不知道
10.f.tell()
0.
siryang
发表于 2023-5-13 17:04:52
0.不会,会创建该文件
1.使用f.fluch()方法
2.从文件对象中读取指定数量的字符(或者遇到 EOF 停止)
3.1
4.不知道
5.文件路径不这样书写?
6.“W”打开会清空原本文件内容,确保文件里面没有内容
7.可以
8.确定从那个位置开始操作
9.文件结束位置
10.f.tell()
0.
蘭蘭蘭
发表于 2023-5-14 14:46:23
+ 1
idudu105
发表于 2023-5-14 17:45:37
1
slevon0911
发表于 2023-5-15 22:56:35
已阅
hanzhengx
发表于 2023-5-17 15:55:49
1111111
794987988
发表于 2023-5-18 13:19:53
不会
liwp
发表于 2023-5-18 15:17:18
1
有其月
发表于 2023-5-19 15:01:18
#0
"不会,它会自动创建一个文件"
#1
"有,用.flush()方法"
#2
"表示读取三个字符"
#3
"写入一行,因为没有往里面加换行符"
#4
"因为readlines本就是会将读取到的字符串按行处理,所以会留下空白行"
#5
"不知"
#6
"注意打开后会清空文件的内容"
#7
"可以"
#8
"指向文件当前的位置"
#9
"end of file:意思是文档的末尾"
#10
"使用tell()方法"
#动动手
#0
f=open(r"C:\Users\86187\Desktop\FishC.txt","r+")
f.seek(10)
text=f.read(5)
c=open(r"C:\Users\86187\Desktop\FishD.txt",'r+')
c.write(text)
c.close()
#1
f.truncate(15)
f.flush()
#2
f=open(r"C:\Users\86187\Desktop\open_myself.py",'r+')
for i in f:
print(i)
#3
jjkkll1223
发表于 2023-5-20 15:58:10
报错解决不了了
游离的季节
发表于 2023-5-21 21:59:11
# 0、不会
# 1、flush
#2、最多读取的字节数
#3、一行
#4、换行即多一行空白行
#5、地址不存在
#6、注意保存数据,用w打开会全部清空
#7、可以
#8、确定文件的读取位置
#9、文件末尾
#10、f.tell()
#动动手:
#0、
print('第0题')
test=[]
f=open(r"D:\jm\python\FishC.txt")
for each in f:
print(each[:])
print(each)
test.append(each)
print(test)
ff=open(r"D:\jm\python\FishD.txt",'w')
ff.writelines(test)
ff.close()
f.close()
#1、
print('第1题')
test=[]
f=open(r"D:\jm\python\FishC.txt")
for each in f:
print(each[:])
print(each[:14])
test.append(each[:14])
print(test)
f.close()
f=open(r"D:\jm\python\FishC.txt","w")
f.writelines(test)
f.close()
''
#2、open_myself.py
print('第2题')
f=open(r"D:\jm\python\FishC.txt")
for each in f:
print(each[:])
f.close()
#3、图片隐写术
print('第3题')
import zipfile
with zipfile.ZipFile('hw.zip','r') as zipobj:#读取压缩包
for filename in zipobj.namelist():#遍历名称
print(filename.encode('cp437').decode('gbk'))#zip只对译成编码cp437和utf-8识别
print('******')
#import zipfile
#with zipfile.ZipFile('hw.zip','r') as zipobj:
# zipobj.extractall('./hw/')#将文件解压提取出来,生成的是乱码的
#with zipfile.ZipFile('./hw/target.zip','r') as zipobj:
# zipobj.extractall('./hw/target1')
import os
import zipfile
filePath = './hw/target.zip'
release_file_dir = 'D:/jm/python/hw/target/'#隐藏的文件和图片的默认解压路径
zip_file_contents = zipfile.ZipFile(filePath, 'r')
zip_file_contents.extractall(release_file_dir)#解压缩ZIP文件
for file in zip_file_contents.namelist():
filename = file.encode('cp437').decode('gbk')#先使用cp437编码,然后再使用gbk解码
print(filename,'%%')#打印文件名
os.chdir(release_file_dir)#切换到目标目录
os.rename(file,filename)#重命名文件
test1=[]
f=open(r"D:\jm\python\hw\target\被隐藏的文件.txt")#被隐藏的文件.txt
m1=f.read()
print(m1,'^^^^')
#f.writelines(['love\n','python\n','love python\n'])
f.close()
ff=open(r"D:\jm\python\hw\target\test.jpg",'a')
n=ff.tell()
print(n,'@@')
m=ff.seek(0,2)
print(m,'**')
ff.write(m1)
mm=ff.tell()
print(mm,'((')
#ff.writelines(test1)
ff.close()
import PIL.Image as Image
img = Image.open("test.jpg")
#ff.writelines(['1','2','3'])
#ff.writelines(['love\n','python\n','love python\n'])
#img.show()
#img.save('new_test.jpg')
ff.close()
urbanlife
发表于 2023-5-22 15:20:58
看看线
liang_xy
发表于 2023-5-23 17:13:12
azkez5921
发表于 2023-5-23 21:52:01
0:
不会,在关闭时会创建
1:
有flush()
2:
读取指针位置后3个字符?
3:
1行
4:
难道遍历语句默认换行么
5:
不明白
6:
不能打开只读文件
7:
应该不能吧,会报错吧
8:
上一次操作的位置指明
9:
end of the file
10:
f.tell()
舟舟舟舟舟自横
发表于 2023-5-24 11:08:36
"""
0.看使用的是什么模式 r会报错 w和a不会
1.f.flush()
2.读取 3个字符
3.1行
4有一个换行,for碰到换行符还会再换一行
5.斜杠被理解成了转义符
6.w打开文件会被重新创建,原文件中的内容会被清空
7.可以
8.指向下一个字符,让整段文字连续
9.文字的结束位
10.f.tell
"""
c = open("fishC.txt","r")
d = open("fishD.txt","a+")
c.seek(10)
d.write(c.read(5))
c.close()
d.close()
c = open("fishC.txt","r+")
x = c.read(15)
c = open("fishC.txt","w")
c.write(x)
c.close()
舟舟舟舟舟自横
发表于 2023-5-24 11:09:06
t = open("open_myself.py","r")
for i in t :
print(i)
t.close()
pic = open("test.jpg", "a+")
zipfile = str(open("target.zip", "r+"))
pic.write(zipfile)
pic.close()