I found this question on the internet,

Create a text file with name filename and about size bytes. The file should contain
an incrementing number on each line starting with 0. (ie) 0, 1, 2, 3, .... as many are required to make the file size
just >= size. Each line should end with windows newline ("\r\n").

I don't think i understand the question really well. I wrote a code but it doesn't generate the file as expected. Can anyone tell me what is to be done?? The assertion codes that are used to check my written code checks whether each line ends with "\r\n" and the use of "\r\n" must be just <= 1.. I don't understand how it can be achieved!

Here's my code!

def create_file_numbers(filename, size):
    count = 0
    f = open(filename,'wb')
    while os.stat(filename).st_size < size:
        count += 1

Edited 2 Years Ago by pyTony: Fixed quote format

Doesn't the f.write('\r\n') have to be indented so it is written for every line, not just once at the end?

I did that! even then i get assertion error! :( looks like the program generates a lot of extra numbers!

The problem is here

while os.stat(filename).st_size < size:

Both input and output are buffered on computers. First create a list of what you want to write and check the size each time. Then use .join() to write the file. Writing each number to the file is inefficient and slower anyway, so keep the data in memory where you have more control.

Also, Python has universal newline support so you can just use "\n".

Edited 2 Years Ago by woooee

First create a list of what you want to write and check the size each time.

But how will i know the exact number at which the file size will satisfy?

okay! i did as you told! can you confirf this?? i used '\r\n' just to satisfy the assertion check! i'm not supposed to change that. I'm truncating at the last with +1 or +2 to satisfy the assertion again!
You were right! fy previos code took around 19 secs to make the 1 MB file (size 1024 * 1024) but this does it in just 0.56 secs
Thanks a lot! Just confirm the code :)

def create_file_numbers(filename, size):
    lis = list()
    count = 0
    while sys.getsizeof(lis) < size:
        count += 1
    f = open(filename,'wb')
    if size <= 1024:
        f.truncate(size + 1)
        f.truncate(size + 2)

I did your task like this, but it can cut the file in middle of the line, and the file will not end with '\r\n'.

def create_file_numbers(filename, size):
    with open(filename,'wb') as f:
        f.write('\r\n'.join(str(n) for n in range(size//2+1))[:size])

create_file_numbers('testnum.txt', 1000)

Bit lazy for the needed range to get to string length also.

Edited 2 Years Ago by pyTony


that's too good! but can you tell me a little about this statement??

I am slicing the first size characters from the joined together number strings. Actually each number is at least one number + two line separators, so we could replace the 2 with 3 without leading to trouble with too little characters.

Edited 2 Years Ago by pyTony

You should add the "\r\n" each time and test for size

def create_file_numbers(filename, size):
    output_list = list()
    count = 0
    total_output = 0
    while total_output < size:
        output = "%d\r\n" % (count)
        total_output += len(output)
        count += 1

    with open(filename,'wb') as f:

Way to fix my code also (997 length splits number, so that is used for test):

def create_file_numbers(filename, size):
    nums = '\r\n'.join(str(n) for n in range(size//3+1))
    while nums[size-1] != '\n':
        size += 1
    with open(filename,'wb') as f:
        assert nums[:size].endswith('\n')

create_file_numbers('testnum.txt', 997)

Edited 2 Years Ago by pyTony

This question has already been answered. Start a new discussion instead.