Python文件创建和打开

文件的创建

创立了一个名为student.text的文件

filename='student.text'
stu_txt = open(filename, 'a', encoding='utf-8')

文件的打开

对文件的读写操作

file=open('aa.txt','r')
print(file.readlines())
file.close()

文件的复制操作

src_file=open('james.png','rb') #把图片变成010101二进制读到
tar_file=open('jamesCopy.png','wb')
tar_file.write(src_file.read())
src_file.close()
tar_file.close()

文件读写的with操作

#with语句-不用再写file.close
with open('james.png','rb') as src_file:
    with open('toCopy.png','wb') as tar_file:
        tar_file.write(src_file.read())

你可能感兴趣的:(python)