python - Find filename with highest integer in name -


summary

if fcr_network_coordinates_0 , fcr_network_coordinates_2 exists, should write file fcr_network_coordinates_3 , not fcr_network_coordinates_1

details

i have following problem:

i want write new csv file, if not exist , increase extension number if file found in directory. if example file number extension "1" exists, , 1 "3", none "2", should write next file "4". should add 1 highest number extension

my code far is:

    index = 0     while os.path.exists('../fcr_network_coordinates_'+ str(index) + '.csv'):         index+=1                 open('../fcr_network_coordinates_'+str(index)+'.csv', 'wb') csv_file:         writer = csv.writer(csv_file, delimiter=";")         key, value in sparse1.items():             writer.writerow(['{:.1f}'.format(t) t in key]+value) 

edit

it should work paths parameters added in path name

 "../fcr_network_coordinates_"+"r_"+radius+"x_"+x+"y_"+y+"z_"‌​+z+"fcr_"+fcr_size+"‌​_"+new_number+".csv"  

could like:

fcr_network_coordinates_radius_3_x_0.3_y_0.3_z_2_fcr_2_1.csv 

edit2

furthermore if there other parameters in filename should not highest number of files, of highest number of file have these parameters too

something following should work you:

import glob import os  # .....  existing_matches = glob.glob('../fcr_network_coordinates_*.csv')  if existing_matches:     used_numbers = []     f in existing_matches:         try:             file_number = int(os.path.splitext(os.path.basename(f))[0].split('_')[-1])             used_numbers.append(file_number)         except valueerror:             pass     save_number = max(used_numbers) + 1 else:     save_number = 1  open('../fcr_network_coordinates_{}.csv'.format(save_number), 'wb') csv_file:     writer = csv.writer(csv_file, delimiter=";")     key, value in sparse1.items():         writer.writerow(['{:.1f}'.format(t) t in key] + value) 

glob finds files names similar pattern, * used wildcard.

we use os.path manipulate each filename , work out number in name is:

  • os.path.basename() gets filename - e.g. 'fcr_network_coordinates_1.csv'
  • os.path.splitext() splits file name ('fcr_network_coordinates_1') extension ('.csv'). taking element @ index 0 gets filename rather extension
  • splitting based on '_' splits every time there '_' - resulting in list of ['fcr', 'network', 'coordinates', '1']. taking index -1 gets last entry in list, i.e. 1.
  • we have wrap int() able apply numeric operations it.

we catch error in case there filename using letters rather numbers after underscore. then, take max of numbers found , add one. if no numbers have been found, use 1 filename.

edit: in response question update, need alter our glob , final name write - glob changes to:

existing_matches = glob.glob('../fcr_network_coordinates_r_{}_x_{}_y_{}_z_{}_fcr_{}_*.csv'.format(     radius, x, y, z, fcr_size)) 

and file opening line changes to:

with open('../fcr_network_coordinates_r_{}_x_{}_y_{}_z_{}_fcr_{}_{}.csv'.format(         radius, x, y, z, fcr_size, save_number), 'wb') csv_file: 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -