0

I have a .txt file with a lot of numbers.

What my code does is read through the file, if it finds a number it adds it to a list called duns. Once the length of duns is 9 it adds these numbers to another list called fileSize. Once the length of fileSize is 50 (so total of 450 numbers), I want to write the content of fileSize in a .txt file seperated by commas. And I want a file to be created for each 450 numbers.

My code basically works. The problem I have is when I want to open the file in which fileSize should be written down.

Here is my code:

from os import chdir
import numbers

chdir("C:\\Users\\u1080476\\Desktop\\Code\\TMO")

filename = "DUNS_par_bloc.txt"

fileSize = []     
duns = []
count = 0
numbers = ['1', '2', '3', '4', '5', '6','7','8','9','0']


f = open(filename, "r")
while True:

    c = f.read(1)
    if not c:
        print("End of File")
        break
    print("read a character: ",c)
    for num in numbers:
        if c == num:
            if len(duns) < 9:
            duns.append(c) 
            else:
                fileSize.append(''.join(duns))
                duns = []
        if len(fileSize) == 50:
             chdir("C:\\Users\\u1080476\\Desktop\\Code\\TMO\\files")
             count += 1

             text_file = open(count,".txt", "a")
             strBigFile = ','.join(fileSize)
             text_file.write(strBigFile)

             text_file.close()
f.close()

The error I get is:

Traceback (most recent call last):
  File "C:\Users\u1080476\Desktop\Code\TMO\read.py", line 33, in <module>
    text_file = open(count,".txt", "a")
TypeError: an integer is required (got type str)
Pierre Corbin
  • 62
  • 2
  • 10

0 Answers0