_______  _______  ______   _______ _________ _______        _________         _________
(  ____ )(  ___  )(  ___ \ (  ___  )\__   __/(  ____ \       \__   __/|\     /|\__   __/
| (    )|| (   ) || (   ) )| (   ) |   ) (   | (    \/          ) (   ( \   / )   ) (   
| (____)|| |   | || (__/ / | |   | |   | |   | (_____           | |    \ (_) /    | |   
|     __)| |   | ||  __ (  | |   | |   | |   (_____  )          | |     ) _ (     | |   
| (\ (   | |   | || (  \ \ | |   | |   | |         ) |          | |    / ( ) \    | |   
| ) \ \__| (___) || )___) )| (___) |   | |   /\____) |   _      | |   ( /   \ )   | |   
|/   \__/(_______)|/ \___/ (_______)   )_(   \_______)  (_)     )_(   |/     \|   )_(   
        
ebobgf . gkg
A digital cybersecurity scrapbook of Het Joshi

The FileGen Project and its why,how,what?

by Het Joshi

What is this project exactly?

This is a tool that generates a random dummy file that is of a specified size. You input the output file name, format and size. After that, BOOM out comes the expected file of that size and name which contains random characters.

Why bother making this?

I was working on a project where I had to test download speeds of different hosting tools. So to measure the metrics, I had to download a 10 MB and 100 MB files using these toolsso I can get comparable results. When I satrted working, I asked my self:

How do I generate a 10 MB file? How do I generate a 100 MB file?

Now my mentor reminded me of the below fact:

Each character in a .py file is 1 B

Now, I didn’t want to type so many letters also so I came up with another method as I will describe in the next section.

The Implementation

I have linked the repository so you can view the whole code in its enterity but the fumnction that writes random bits below:

def generate_file(file_name, size, unit):
    """
    Generates a file of the specified size filled with random data.

    Parameters:
    file_name (str): The name of the file to be created.
    size (float): The size of the file.
    unit (str): The unit of the file size (b, kb, mb, gb).
    """
    size_bytes = convert_size_to_bytes(size, unit)
    with open(file_name, 'wb') as f:
        f.write(os.urandom(size_bytes))
    print(f'File "{file_name}" of size {size} {unit.upper()} ({size_bytes} bytes) has been created.')
    print(f'Output file path: {os.path.abspath(file_name)}')

The above function takes in the file name, size and unit as parameters.

The important line here is f.write(os.urandom(size_bytes)).

Other functions:

Converts the size from the specified unit to bytes.

Parameters:

size (float): The size of the file.
unit (str): The unit of the file size (b, kb, mb, gb).

Returns:

int: The size of the file in bytes.

Generates a file of the specified size filled with random data.

Parameters:

file_name (str): The name of the file to be created.
size (float): The size of the file.
unit (str): The unit of the file size (b, kb, mb, gb).

print_instructions() Prints the instructions for using the script.

Conclusion:

If you face a problem, make your own solution so you learn a new concept and put it in the public domain so other can learn and use it too!