I have a folder which contains numpy files. I need to create a numpy list where I will put all my numpy file names. For that, I use this code:
import os
a = open('C:\\Users\\user\\My_Test_Traces\\Traces.list_npy', "w")
for path, subdirs, files in os.walk(r'C:\\Users\\user\\CPA_test_1000_Tests\\test'):
for filename in files:
f = os.path.join(path, filename)
a.write(str(f) + os.linesep)
As a result, it gives me a list of my files and their paths like this:
C:\\Users\\user\\My_Test_Traces\\ Trace1_Pltx1
C:\\Users\\user\\My_Test_Traces\\ Trace2_Pltx2
C:\\Users\\user\\My_Test_Traces\\ Trace3_Pltx3
C:\\Users\\user\\My_Test_Traces\\ Trace4_Pltx4
however , I need only the files name, I don't need any spaces between lines:
Trace1_Pltx1
Trace2_Pltx2
Trace3_Pltx3
Trace4_Pltx4
tyhat有一个功能可以完全满足您的需要,您应该尝试以下操作:
os.path.basename(path)
You can use,
from os.path import basename
print basename('path/file.txt')
If you don't want extension, refer the post
Maybe this is better:
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)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.