Scenario: I have a list of strings that contains multiple file names. I am trying to either create a new list with just a part of each name, or to edit the original list to have just the part I wan. For example:
Original first item of list:
"iteration5_test1_part1.txt"
Wanted result:
"iteration5"
(basically I want to keep only what comes before the first underscore)
Although I read here How to modify list entries during for loop? that it is not good practice to edit a list, I don't know any others ways to do it, so I am trying:
mypath = "/DGMS/Destop/uploaded"
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for f in onlyfiles:
f = f.split("_")
f = f[0]
But unfortunately, this does not change the list in the wanted way.
Question: What am I doing wrong?