1、常用的文件打开模式
r
file=open('a.txt','r')
print(file.readlines())
file.close()
w
file=open('a.txt','w')
file.write('whywhywhy')
file.close()
a
file=open('a.txt','a')
file.write('whywhywhy')
file.close()
rb wb
a=open('wu.png','rb')
b=open('copya.png','wb')
b.write(a.read())
a.close()
b.close
2、文件对象的常用方法
读
file=open('a.txt','r')
print(file.read(2))
print(file.readline())
print(file.readlines())
file.close()
写
file=open('a.txt','w')
l=['i','am','sure']
file.writelines(l)
file.write('i am sure')
file.close()
flush
file=open('a.txt','w')
file.write('even')
file.flush()
file.write('though')
file.close()
seek
file=open('a.txt','r')
file.seek(3)
print(file.read())
file.close()
3、with语句
with open('a.txt','r') as file:
print(file.read())
4、os模块操作系统文件
import os
os.system('calc.exe')
os.system('notepad.exe')
os.startfile('D:\\Wechat\\Wechat.exe')
5、os模块操作目录相关函数
listdir(path)
import os
l=os.listdir('../pythonProject')
print(l)
getcwd()
import os
print(os.getcwd())