# CODING=GBK
Import OS
Import Os.path
#读取目录下的所有文件, including nested folders
Def getfilelist (dir, fileList):
Newdir = Dir
if os.path.isfile (dir):
Filelist.append (dir)
Elif Os.path.isdir (dir):
for in Os.listdir (dir):
# If you need to ignore some folders, use the following code
if "xxx":
Continue
Newdir = Os.path.join (dir, s)
Getfilelist (Newdir, FileList)
return fileList
"E:\\differnernt_size_digit_data\\resize\\train\\28x28"
List = Getfilelist (Filedir, [])
# Open a file
fo = open ("file_list.txt""W") # opening file
for in list:
Print (i) # Test full file path
Print (Os.path.basename (i)) # file name
index = I.find (".", 0) # Find the location of the dot
Print (I[index-1:index]) # intercept target characters
" " + I[index-1:index]) # test Target string
" " "\ n") # write the target string to the file
Fo.close () # Close Open File
Python reads everything under a file, gets a filename, intercepts characters, writes back to a file