REF:
# -*- coding: utf-8 -*-
import os
TARGETPATH = r'C:\\Users\\.ASIA\\Downloads'
TARGETPATH = r'C:\Users\.ASIA\Documents\Work\PAD'
records = []
for currentDir, _, includedFiles in os.walk(TARGETPATH):
#if not currentDir.endswith('*'): continue
#else:
records.append(currentDir) # 将以“_BAD”结尾的文件夹名加入records
records.extend(includedFiles) # 将该文件夹内的文件名列表扩展到records
# 将records写入.txt
txtFile = open(os.path.join(TARGETPATH, '02_04_BAD.txt'), 'w',encoding='utf-8')
txtFile.write((os.linesep.join(records)))
txtFile.close()
# 将排序后的records写入.txt
with open(os.path.join(TARGETPATH, '02_04_BAD_SORTED.txt'), 'w',encoding='utf-8') as txtFile:
txtFile.write('\n'.join(sorted(records)))
import os
TARGETPATH = r'C:\\Users\\.ASIA\\Downloads'
TARGETPATH = r'C:\Users\.ASIA\Documents\Work\Pad'
records = []
txtFile = open(os.path.join(TARGETPATH, '02_04_BAD.txt'), 'w',encoding='utf-8')
for currentDir, _, includedFiles in os.walk(TARGETPATH):
#if not currentDir.endswith('*'): continue
#else:
records.append(currentDir) # 将以“_BAD”结尾的文件夹名加入records
records.extend(includedFiles) # 将该文件夹内的文件名列表扩展到records
# 将records写入.txt
txtFile.write('\t')
txtFile.write((os.linesep.join(records)))
txtFile.close()
# 将排序后的records写入.txt
#with open(os.path.join(TARGETPATH, '02_04_BAD_SORTED.txt'), 'w',encoding='utf-8') as txtFile:
# txtFile.write('\n'.join(sorted(records)))
txtFile_2 = open(TARGETPATH + '\\02_04_BAD_SORTED.txt', 'w',encoding='utf-8')
txtFile_1 = open(TARGETPATH + '\\02_04_BAD.txt', 'r',encoding='utf-8')
content = txtFile_1.readlines()
print(type(content))
print(content)
result_1 = 5
for i in content:
zifu = '\\'
result = i.count(zifu)
#print(i)
print(result)
if result > 0 :
result_1 = result
for j in range (result_1 - 5): #路径中已经有5个“\”,所以先减去
txtFile_2.write('\t')
elif (result == 0) :
result_1=result_1
for j in range (result_1 - 4):
txtFile_2.write('\t')
txtFile_2.write(i)
txtFile_1.close()
txtFile_2.close()