1

How to create a file list of my files included in the same folder? In this question, I have asked about how to put all my file names from the same folder in one numpy file.

import os
path_For_Numpy_Files = 'C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'
with open('C:\\Users\\user\\My_Test_Traces\\Traces.list_npy', 'w') as fp:
    fp.write('\n'.join(os.listdir(path_For_Numpy_Files)))

I have 10000 numpy files in my folder, so the result is:

   Tracenumber=01_Pltx1
   Tracenumber=02_Pltx2
   Tracenumber=03_Pltx3
   Tracenumber=04_Pltx4
   Tracenumber=05_Pltx5
   Tracenumber=06_Pltx6
   Tracenumber=07_Pltx7
   Tracenumber=08_Pltx8
   Tracenumber=09_Pltx9
   Tracenumber=10_Pltx10
   Tracenumber=1000_Pltx1000
   Tracenumber=100_Pltx100
   Tracenumber=101_Pltx101

The order is very important to analyse my result, how to keep thqt order when creating the list please? I mean that I need my results like this:

   Tracenumber=01_Pltx1
   Tracenumber=02_Pltx2
   Tracenumber=03_Pltx3
   Tracenumber=04_Pltx4
   Tracenumber=05_Pltx5
   Tracenumber=06_Pltx6
   Tracenumber=07_Pltx7
   Tracenumber=08_Pltx8
   Tracenumber=09_Pltx9
   Tracenumber=10_Pltx10
   Tracenumber=11_Pltx11
   Tracenumber=12_Pltx12
   Tracenumber=13_Pltx13

I try to iterate it by using:

import os
path_For_Numpy_Files = 'C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'
with open('C:\\Users\\user\\My_Test_Traces\\Traces.list_npy', 'w') as fp:
    list_files=os.listdir(path_For_Numpy_Files)
    list_files_In_Order=sorted(list_files, key=lambda x:(int(re.sub('D:\tt','',x)),x))
    fp.write('\n'.join(sorted(os.listdir(list_files_In_Order))))

It gives me this error:

invalid literal for int() with base 10: ' Tracenumber=01_Pltx1'

How to solve this problem please?

Community
  • 1
  • 1
  • Possible duplicate of [Numeric Sort in Python](http://stackoverflow.com/questions/3426108/numeric-sort-in-python) – Bemmu Mar 20 '17 at 15:25
  • @Bemmu, Thank you but it isn't the same case. –  Mar 20 '17 at 15:28
  • If you first sort os.listdir results numerically, would that not be your solution? – Bemmu Mar 20 '17 at 15:29
  • 1
    If not, another thing you can try is to make a custom sort function that extracts the final number part from the filenames and sorts based on that. – Bemmu Mar 20 '17 at 15:30
  • @Bemmu, Could you please give me more explanation for your idea? –  Mar 20 '17 at 16:01
  • In your attempt, list_files.sort() sorts the list in-place, it does not return a new sorted list. Also to do the numeric sort, use list_files.sort(key=int) – Bemmu Mar 20 '17 at 16:03
  • @Bemmu, it gives me this error: invalid literal for int() with base 10: –  Mar 20 '17 at 16:19
  • Remove leading zeroes from the number. If a number starts with a zero, it is assumed to be octal instead of base 10. – Bemmu Mar 21 '17 at 03:32
  • I have the same error. –  Mar 21 '17 at 07:49

1 Answers1

1

I edit the solution, It may work now: You will sort your files based on time.

import os
path_For_Numpy_Files = 'C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'
path_List_File='C:\\Users\\user\\My_Test_Traces\\Traces.list_npy'
with open(path_List_File, 'w') as fp:
    os.chdir(path_For_Numpy_Files)
    list_files=os.listdir(os.getcwd())
    fp.write('\n'.join(sorted((list_files),key=os.path.getmtime)))