1

How to create a file list of my files included in the same folder? I have posted in this question that I need to put all mye file names from the same folder in one numpy file.

import os
folder = 'C:\\Users\\user\\CPA_test_1000_Tests\\test'
with open('C:\\Users\\user\\My_Test_Traces\\Traces.list_npy', 'w') as fp:
    fp.write(os.linesep.join(os.listdir(folder)))

I use this code and it works, but my still with space line between names of files, I mean that the result that I have looks like this:

   Trace1_Pltx1

   Trace2_Pltx2

   Trace3_Pltx3

   Trace4_Pltx4

However the result that I must I have, must look like this:

   Trace1_Pltx1
   Trace2_Pltx2
   Trace3_Pltx3
   Trace4_Pltx4 

I don't know really what is the problem?

Community
  • 1
  • 1

1 Answers1

1

Take a look at os.linesep() method that you're calling...

As stated in the docs:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.

So, instead of:

fp.write(os.linesep.join(os.listdir(folder)))

try:

fp.write('\n'.join(os.listdir(folder)))

Or you can even use

with open('C:\\Users\\user\\My_Test_Traces\\Traces.list_npy', 'w') as fp:
    for f in os.listdir(folder):
        fp.write(f + "\n")
dot.Py
  • 5,007
  • 5
  • 31
  • 52