Python writes folder path to txt text and encounters Permission denied problem

The specific problem is:
I have a permission error in the middle of writing a file. I have a lot of files. I have to write more than 8,000 lines. No error was reported when I wrote two or three thousand lines.

The file structure is

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

Purpose: Write the path of the file in the class folder

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'

Change a to a+ and no error will be reported

Guess you like

Origin blog.csdn.net/qq_38469784/article/details/108923624