0%

python-文件操作

python的课后作业

题一:

1
2
f =  open("python.txt","a")
f.write("python")

题二:

1
2
3
4
5
6
import os

f = open("python.txt","a")
f.write("python")
f.close()
os.rename("python.txt","python_NEW.txt")

题三:

1
2
3
4
5
6
7
f =  open("python.txt","a")
f.write("python")
with open("python.txt") as f:
file = f.readline()
print(file)
file1 = file.swapcase()
print(file1)

或者

1
2
3
4
5
6
7
8
f =  open("python.txt","a+")
f.write("python")
f.seek(0) #将指针归0,也就是复原到第一位,读取是从指针往后读,指针在最后,他后面就没有东西了
file = f.readline()
print(file)
file1 = file.swapcase()
print(file1)
f.close()