import os
import tqdm
list_name = []
path_dir = ['sofile_ads_og_pt']
path_so = "/Users/PycharmProjects/"
def listdir(path,list_name): # 传入存储的list
for file in os.listdir(path):
filecheck = file.split('.')
if filecheck[0] == 'so' and filecheck[len(filecheck)-1] == 'log':
file_path = os.path.join(path, file)
# if os.path.isdir(file_path):
# listdir(file_path, list_name)
# else:
# list_name.append(file_path)
list_name.append(file_path)
print(list_name)
list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
print(list_name)
print("载入文件完成:",len(list_name))
if __name__ == '__main__':
for dirname in (path_dir):
list_name.clear()
path_so_all = path_so + dirname
listdir(path_so_all,list_name)
print("main部分:")
print(list_name)
list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
print(list_name)
print(path_so_all)
import os
import tqdm
list_name = []
path_dir = ['sofile_ads_og_pt']
path_so = "/Users/PycharmProjects/"
def listdir(path,list_name): # 传入存储的list
for file in os.listdir(path):
filecheck = file.split('.')
if filecheck[0] == 'so' and filecheck[len(filecheck)-1] == 'log':
file_path = os.path.join(path, file)
# if os.path.isdir(file_path):
# listdir(file_path, list_name)
# else:
# list_name.append(file_path)
list_name.append(file_path)
print(list_name)
list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
print(list_name)
print("载入文件完成:",len(list_name))
if __name__ == '__main__':
for dirname in (path_dir):
list_name.clear()
path_so_all = path_so + dirname
listdir(path_so_all,list_name)
print("main 部分:")
print(list_name)
list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
print(list_name)
print(path_so_all)
output:
listdir funtion:
list_name = [not sorted]
list_name = [sorted]
main funtion:
list_name = [not sorted]
list_name = [sorted]
why the main part of list_name is not sorted ???
And if the list_name is local variable, why global list_name have items ???