在 os.walk 函数的结果中,我得到了这个列表,其中包含文件夹中的文件名:
['tr-02-10-18.pdf', 'tr-02-11-18.pdf', 'tr-02-12-18.pdf', 'tr-03-11-18.pdf', 'tr-03-12-18.pdf', 'tr-04-10-18.pdf', 'tr-04-11-18.pdf', 'tr-04-12-18.pdf']
我的目标是将列表带到该表单:
['tr/02/10/18.pdf', 'tr/02/11/18.pdf','tr/02/12/18.pdf', 'tr/03/11/18.pdf', 'tr/03/12/18.pdf', 'tr/04/10/18.pdf', 'tr/04/11/18.pdf', 'tr/04/12/18.pdf']
我尝试使用如下所示的代码:
import os
for file_name in os.walk(input()):
egg1=(str(file_name[-1]))
egg2= []
for mark in egg1:
mark=[i.replace("-","/") for i in mark]
egg2.append(mark)
print(egg2)
我得到的是这个列表:
[['['], ["'"], ['t'], ['r'], ['/'], ['0'], ['2'], ['/'], ['1'], ['0'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['2'], ['/'], ['1'], ['1'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['2'], ['/'], ['1'], ['2'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['3'], ['/'], ['1'], ['1'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['3'], ['/'], ['1'], ['2'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['4'], ['/'], ['1'], ['0'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['4'], ['/'], ['1'], ['1'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [','], [' '], ["'"], ['t'], ['r'], ['/'], ['0'], ['4'], ['/'], ['1'], ['2'], ['/'], ['1'], ['8'], ['.'], ['p'], ['d'], ['f'], ["'"], [']']]
PS这是我的第一个问题,如有不一致之处请见谅