Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For every iteration in my loop for, I need to give 'the number of my iteration' as a name for the file, for example, the goal is to save:

my first iteration in the first file.
my second iteration in the second file.
....

I use for that the library numpy, but my code doesn't give me the solution that i need, in fact my actual code oblige me to enter the name of the file after each iteration, that is easy if I have 6 or 7 iteration, but i am in the case that I have 100 iteration, it doesn't make sense:

for line, a in enumerate(Plaintxt_file):
    #instruction
    #result
    fileName = raw_input()
    if(fileName!='end'):
        fileName = r'C:\Users\My_resul\Win_My_Scripts\'+fileName
        np.save(fileName+'.npy',Result)
ser.close()

I would be very grateful if you could help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

Create your file name from the line number:

for line, a in enumerate(Plaintxt_file):
    fileName = r'C:UsersMy_resulWin_My_Scriptsfile_{}.npy'.format(line)
    np.save(fileName, Result)

This start with file name file_0.npy. If you like to start with 1, specify the starting index in enumerate:

for line, a in enumerate(Plaintxt_file, 1):

Of course, this assumes you don't need line starting with 0 anywhere else.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...