In result of os.walk function I get this list which contain names of files in folder:
['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']
My goal is to bring the list to that form:
['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']
I've tried to use code as seen below:
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)
What I got is this list:
[['['], ["'"], ['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'], ["'"], [']']]
P.S. This is my first question ever, so sorry for any inconsistency
You are making several mistakes:
os.walk()
produces a tuple of 3 elements, the path to the current directory, a list of filenames and a list of directory names. You named the tuple file_name
:
for file_name in os.walk(input()):
Better to split that out into separate components, so for dirpath, dirnames, filenames in os.walk(...):
. Since you ignore the first two arguments, you can also use for *_, filenames in os.walk(...):
You turned the list of filenames into a single string:
egg1=(str(file_name[-1]))
file_name[-1]
is the list of filenames. You turned the list into a string, so now you have string representations and [
, ]
and ,
comma characters:
>>> egg1
"['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']"
You then take each individual character from the above string and replaced dashes with slashes. Don't turn lists into strings, iterate directly over the list.
You replace the egg2
list with a new, empty list for each directory. Create the list before the os.walk()
loop.
You put the result of each str.replace()
call into a new list with a single element, to then append that list to the egg2
list. Don't add more lists.
This code works:
result = []
for *_, filenames in os.walk(input()):
for filename in filenames:
replaced = filename.replace('-', '/')
result.append(replaced)
This can be combined into a list comprehension:
result = [f.replace('-', '/') for *_, fs in os.walk(input()) for f in fs]