Reputation: 49
I have many files in a folder. I want to pick the latest file. I have written code also, but it is givving values in separate list.
Output I'm getting:
['tb_exec_ns_decile_20190129']
['tb_exec_ns_decile_20190229']
['tb_exec_ns_decile_20190329']
Expected Output:
['tb_exec_ns_decile_20190129', 'tb_exec_ns_decile_20190229', 'tb_exec_ns_decile_20190329']
Code:
path1 = "D:/Users/SPate233/Downloads/testing/*.csv"
files = glob.glob(path1)
print(files)
for name in files:
new_files = []
new_files = os.path.split(name)[1].split('.')[0]
new_files = new_files.split(',')
print(new_files)
Upvotes: 0
Views: 65
Reputation: 797
You can always merge list in python as in example below:
>>> first_list = [1, 2, 3]
>>> second_list = [4, 5, 6]
>>> merged_list = first_list + second_list
>>> merged_list
[1, 2, 3, 4, 5, 6]
Upvotes: 1
Reputation: 16792
The correct term here would be append
not merge
since, you want all the file names in a list together, Create an empty list to store all the file names in it:
f_list = [] # an empty list to store the file names
for name in files:
file_name = os.path.split(name)[1].split('.')[0]
f_list.append(file_name.split(',')) # appending the names to the list
print(f_list) # print the list
Upvotes: 4