Home > Mobile >  To create a list of all file name & it's data. The first element of list should be the file nam
To create a list of all file name & it's data. The first element of list should be the file nam

Time:02-01

Note, the first element of the list should be the filename and second element should be it's corresponding data.

And I've to do this for all the files in the file_path

for i in file_path:
    txt_file=open(i, 'r')
    data = txt_file.read()
    k=0 
    filename[k] = filename[k] ':' data
    k =1
        
    txt_file.close()

filename = ['PATIENT#3 PAGE 1.txt','PATIENT #3 PAGE 2.txt','PATIENT #5 PAGE 1.txt','PATIENT #5 PAGE 2.txt',..]

file_path = ['C:\Users\OCR_output\PATIENT #3 PAGE 1.txt,'C:\Users\OCR_output\PATIENT #3 PAGE 2.txt']

My output needs to look like this: [filename1, 'the text data inside file1', 'filename2', 'the text data inside file2']

Please help me to get the desired output. Thanks!

CodePudding user response:

So first things first: you are updating k in every iteration of the for-loop so it always becomes 0. So you are only updating the first element. We can fix this by simply putting the k outside of the loop:

k=0 
for i in file_path:
    txt_file=open(i, 'r')
    data = txt_file.read()
    filename[k] = filename[k] ':' data
    k =1
        
    txt_file.close()

Now I'm going to show a few improvements to this code, assuming the description you have given. First, let's use a with statement to avoid manually opening and closing the file:

k=0
for i in file_path:
    with open(i, 'r')as txt_file:
        data = txt_file.read()
        filename[k] = filename[k] ':' data
        k =1

furthermore, you can use append to add data to your collection, which I'm assuming you want:

for path in file_path:
    with open(path, 'r')as txt_file:
        data = txt_file.read()
        filename.append(i)
        filename.append(data)
        # or: filename.extend([i, data])
  •  Tags:  
  • Related