vendredi 25 janvier 2019

if statment based on part of string in Python

I have a fast question (i hope so). I'm learning a python and I have a problem with if statement based on part of a string. I'm looking for an answer but I didn't find anything useful.

I have a list of few (15) names grouped in 3 subgroups:

'axxxxxxxx_44'
'bxxxxxxxx_22'
'cxxxxxxxx_2'

i've created a few methods for editing that files and now I put it into 'for' loop. For every edited subgrup is created separated directory and i need to put the result depends of _xx.

How can i do that by if statement?

Now all my files are generated in same, main directory.

Code for example:

try:
    for i in range(0, len(dir_names)):
        os.mkdir(dir_names[i])
except FileExistsError:
    print("Directory ", dir_names[i], " already exists")

for x in range(0, len(file_names)):
    fileName = '{}'.format(file_names[x])
#here i need (i know how to change dir but problem with if on string):
#if _44 change working directory on directory named _44
#else if _22 change working directory on directory named _22
# else _02 to _02
    fileIn = fileName + "{}".format('')
    fileFixed = fileName + "{}{}".format('_out', '.txt')
    fileFinalCSV = fileName + "{}{}".format('_out', '.csv')

Sorry for bad practice if they are :D but I'm just starting :( Can someone help with this problem?

Aucun commentaire:

Enregistrer un commentaire