user6652926
user6652926

Reputation:

How to put the name of file list in order from the same folder in the same numpy file?

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?

Upvotes: 1

Views: 57

Answers (1)

user6594048
user6594048

Reputation:

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)))

Upvotes: 1

Related Questions