import os
src_path = r"D:\test\func.wmv"
dest_path = r"D:\test\func2.wmv"
handle_read = open(src_path,"rb")
handle_write = open(dest_path,"wb")
file_size = os.path.getsize(src_path)
has_read = 0
while has_read <= file_size:
res = handle_read.read(1024)
print(res)
handle_write.write(res)
handle_write.flush()
has_read += 1024
print("读取完毕!")
handle_read.close()
handle_write.close()
1.向csv文件中写入数据
(1) 列表的方式写入
import csv
with open('data.csv','a+',encoding='utf-8',newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([1,2,3,4,5,6,7,'你好'])
writer.writerows([[0,1,3],[1,2,3]])
(2) 字典方式写入
import csv
with open('data2.csv','a+',encoding='utf-8',newline='') as csvfile:
filename = ['name','sex']
writer = csv.DictWriter(csvfile,filename)
writer.writeheader()
writer.writerow({"name":"张三","sex":"男"})
writer.writerow({"name": "李四", "sex": "男"})
writer.writerow({"name": "王五", "sex": "男"})
2.读取csv文件的内容
(1) 列表的方式读取
import csv
with open('data.csv','r',encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row,type(row),row[1])
(2) 字典的方式读取
import csv
with open('data2.csv','r',encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['name'])