Reputation: 111
I am trying to rename all the items in the list by removing the '.csv' extension from each item
The following code removes the extension in the variable new but does not replace each with new:
csv = ['NC_hello1.csv', 'NC_hell02001.csv', 'NC_hello20002.csv']
for each in csv:
new = os.path.splitext(each)[0]
each = new
print each
How do I get the following output?
NC_hello1
NC_hell02001
NC_hello20002
Upvotes: 4
Views: 5217
Reputation: 19601
Try something like this:
csv = [os.path.splitext(each)[0] for each in csv]
Alternatively, if you need to further process the modified filename, e.g. print it like in the code above, access the list by indexing its elements:
for index,each in enumerate(csv):
new = os.path.splitext(each)[0]
csv[index] = new
print new
Upvotes: 8