python写入文件夹路径到txt文本中遇到Permission denied问题

具体问题是:写入文件到中途,出现权限错误
我的文件比较多,要写入8000多行,之前两三千行的时候没有报错

文件结构是

全部文件夹
   一类文件夹(8000个文件夹
   二类文件夹(2000个
   三类文件夹(2500个
   四类文件夹(7000个

目的:写入类文件夹内的 文件的路径

import os
dst_path = "D:\\xxx\\xxx\\data\\crop_jpg\\"
# os.listdir(file)会历遍文件夹内的文件并返回一个列表
path_list = os.listdir(dst_path)
# print(path_list)
for class_name in os.listdir(dst_path):
    class_path = os.path.join(dst_path,class_name)
    class_name_txt = str(class_name) + "list.txt"
    for file_name in os.listdir(class_path):
        file_path = os.path.join(class_path,file_name)
        #with open(str(class_name)+"list.txt","a") as f:
        with open(class_name_txt,"a+") as f:
            f.write(str(class_name)+"/"+file_name+".mp4"+"\n")
    f.close()
Traceback (most recent call last):
  File "read2txt.py", line 45, in <module>
    with open(class_name_txt,"a") as f:
PermissionError: [Errno 13] Permission denied: 'penjianlist.txt'

把a改成a+就不报错了

猜你喜欢

转载自blog.csdn.net/qq_38469784/article/details/108923624