【求助】os.listdir(path)偶遇返回list与实际不一样的情况
使用pycharm tkinter做一个类似于everything的小程序。当全盘扫描到D盘时,os.listdir返回的list与实际目录不一致;但是当单独扫描D盘时,返回的结果是一致的。求大神帮忙解决,感激不尽!!!
我做了个demo测试程序如下:
一、当全盘扫描时
def get_disklist(self):
disk_list = []
if (self.path):
disk_list.append(self.path)
else:
for c in string.ascii_uppercase:
disk = c + ':'
if os.path.isdir(disk):
disk_list.append(disk)
print("The current disk is : %s." % disk_list)
return disk_list
def search_file(self, search_key):
disk_list = self.get_disklist()
allFileList = []
cFlag = 0
dFlag = 0
eFlag = 0
fFlag = 0
for disk in disk_list:
if (disk.lower()[0] == 'c' and cFlag == 0):
cFlag = 1
files = os.listdir(disk)
print("path = %s, name = %s" % (disk, files))
if (disk.lower()[0] == 'd' and dFlag == 0):
dFlag = 1
files = os.listdir(disk)
print("path = %s, name = %s" % (disk, files))
if (disk.lower()[0] == 'e' and eFlag == 0):
eFlag = 1
files = os.listdir(disk)
print("path = %s, name = %s" % (disk, files))
if (disk.lower()[0] == 'f' and fFlag == 0):
fFlag = 1
files = os.listdir(disk)
print("path = %s, name = %s" % (disk, files))
输出结果如下:
path = C:, name = ['$Recycle.Bin', '$SysReset', 'config.ini', 'defaults', 'Documents and Settings', 'HG', 'hiberfil.sys', 'Intel', 'KDubaSoftDownloads', 'LenovoDrivers', 'pagefile.sys', 'pefdata.dat', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'swapfile.sys', 'System Volume Information', 'Users', 'Windows']
path = D:, name = ['.idea', 'ImitateEverything.py', 'venv', '__pycache__']
path = E:, name = ['$RECYCLE.BIN', 'BaiduNetdiskDownload', 'Codes', 'DHC_Repository', 'LotterytTicket', 'QT', 'QTCodes', 'QTSelf', 'System Volume Information', '任务分配列表', '你妹', '简历', '视频']
path = F:, name = ['$RECYCLE.BIN', '85Codes', 'A9 网络环境.docx', 'BaiduNetdiskDownload', 'Codes', 'GitChatCodes', 'msgdispose.log', 'RtcServer.log', 'System Volume Information', '总结', '杂项', '网址以及路径.txt', '资料']
只有D盘执行os.listdir(path)的时候不正确,其他盘都是正确的。
二、当只扫描D盘时,执行一种同样的代码,输出结果却是正确的,输出结果如下;
path = D:/, name = ['$RECYCLE.BIN', 'linshi.txt', 'mydatabase.db', 'Program Files (x86)', 'Program_Develop', 'Program_System', 'PythonProject', 'PythonTest', 'QT', 'Software', 'System Volume Information', 'Video_2019-05-07_173602.wmv', '软件']