简体   繁体   中英

How to write multiple output text file from multiple input text file with different name by using glob.glob in python?

I am a beginner in python. I want to write 50 output file from 50 input text file in python. For example, I have input text file like:

bottom_0cm_Box_0_B_90_wave_1.txt
bottom_0cm_Box_45_B_135_wave_1.txt
bottom_0cm_Box_90_B_180_wave_1.txt
...
top_0cm_Box_0_B_90_wave_1.txt
top_0cm_Box_45_B_135_wave_1.txt
top_0cm_Box_90_B_180_wave_1.txt
...

Inside the one file, I have 1000 number of events, I have to do some calculations for each event, so I am looping over all 1000 events. I have following code:

file = glob.glob("/position/*.txt")
print file
print len(file)
for f in file:
    with open(f, 'rb') as input:
        all_lines = [line for line in input]
    x = np.arange(1,1025)   #x value is common for all the events
    Amplitude=list()
    for j in range(1,1000): 
        y2 = np.array(all_lines[1032*j+8:1032*(j+1)],dtype=float)
        x22 = list(x)
        y2_=list(y2)
        y22 = [((i / 4096)-0.5) for i in y2_] 
        min_y2 = min(y22)  # Find the maximum y2 value
        index_min_y2 = y22.index(min_y2)    #index for minimum of y2
        min_x2 = x[y22.index(min_y2)]
        print 'minimum x2', min_x2, 'minimum y2', min_y2
        Amplitude.append(round(min_y2, 2))
    with open ('bottom_0cm_Box_0_B_90_amplitude.txt', 'w') as fo:
       for d in Amplitude:
         fo.write(str(d) + '\n')

I want to write:

  • input text file with the name bottom_0cm_Box_0_B_90_amplitude.txt of output file bottom_0cm_Box_0_B_90_wave_1.txt .
  • input file name with bottom_0cm_Box_45_B_135_amplitude.txt of output file name bottom_0cm_Box_45_B_135_wave_1.txt

and so on.

You can split the names of the input files into base names and extension names, replace the base names with a different suffix, concatenate them to a full name, and then join the base name with a new directory name for a full path name.

Add:

import os.path

Then change:

with open ('bottom_0cm_Box_0_B_90_amplitude.txt', 'w') as fo:

to:

basename, ext = os.path.splitext(os.path.basename(f))
basename = basename[:-len('wave_1')] + 'amplitude'
with open (os.path.join(new_dir, basename + ext), 'w') as fo:

Moreover, you don't need to save all your results into the Amplitude list since all you're doing afterwards is to dump it out to a file. You should instead write what you're appending to Amplitude directly to your output file to save memory:

import os.path
import glob
file = glob.glob("/position/*.txt")
print file
print len(file)
for f in file:
    with open(f, 'rb') as input:
        all_lines = [line for line in input]
    x = np.arange(1,1025)   #x value is common for all the events
    basename, ext = os.path.splitext(os.path.basename(f))
    basename = basename[:-len('wave_1')] + 'amplitude'
    with open(os.path.join(new_dir, basename + ext), 'w') as fo:
        for j in range(1,1000):
            y2 = np.array(all_lines[1032*j+8:1032*(j+1)],dtype=float)
            x22 = list(x)
            y2_=list(y2)
            y22 = [((i / 4096)-0.5) for i in y2_]
            min_y2 = min(y22)  # Find the maximum y2 value
            index_min_y2 = y22.index(min_y2)    #index for minimum of y2
            min_x2 = x[y22.index(min_y2)]
            print 'minimum x2', min_x2, 'minimum y2', min_y2
            fo.write(str(round(min_y2, 2)) + '\n')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM