csv追加写入的时候报错Permission denied
作者:互联网
是把对应的csv打开了,关闭之后运行就没问题,
把一个csv文件打乱顺序拆分为指定百分比的训练集和测试集,实现如下:
import csv
import os
import random
def split_csv(path, total_len, per):
# 如果train.csv和vali.csv存在就删除
if os.path.exists('E:/Server/U2S_fake_face_test_0909_train.csv'):
os.remove('E:/Server/U2S_fake_face_test_0909_train.csv')
if os.path.exists('E:/Server/U2S_fake_face_test_0909_test.csv'):
os.remove('E:/Server/U2S_fake_face_test_0909_test.csv')
with open(path, 'r', newline='') as file:
csvreader = csv.reader(file)
train_num = round(total_len * per / 100)
index = random.sample(list(range(total_len)), train_num)
print('index', len(index), index,)
i = 0
for ind in csvreader:
if i in index:
# train.csv存放路径
csv_path = os.path.join("E:\\Server", 'U2S_fake_face_test_0909_train.csv')
# print(csv_path)
# 不存在此文件的时候,就创建
if not os.path.exists(csv_path):
with open(csv_path, 'w', newline='') as file:
csvwriter = csv.writer(file)
csvwriter.writerow(ind)
i += 1
# 存在的时候就往里面添加
else:
with open(csv_path, 'a', newline='') as file:
csvwriter = csv.writer(file)
csvwriter.writerow(ind)
i += 1
else:
# vali.csv存放路径
csv_path = os.path.join("E:\\Server", 'U2S_fake_face_test_0909_test.csv')
# print(csv_path)
# 不存在此文件的时候,就创建
if not os.path.exists(csv_path):
with open(csv_path, 'w', newline='') as file:
csvwriter = csv.writer(file)
csvwriter.writerow(ind)
i += 1
else:
with open(csv_path, 'a', newline='') as file:
csvwriter = csv.writer(file)
csvwriter.writerow(ind)
i += 1
print("训练集和验证集分离成功")
return
if __name__ == '__main__':
path = "E:/Server/U2S_fake_face_test_0909.csv"
total_len = len(open(path, 'r').readlines())# csv文件行数
per = 59# 分割比例%
split_csv(path, total_len, per)
标签:Permission,csvwriter,报错,file,denied,test,path,csv,os 来源: https://blog.csdn.net/qq_35037684/article/details/120214905