Home > Software engineering >  How do I make .tmp files (of a given size) with batch and Python
How do I make .tmp files (of a given size) with batch and Python

Time:07-27

I am trying to make 20 .tmp files in batch or python. I have been looking through everything and cant find a solution. I want the .tmp files to store in C:\Users\%USERNAME%\AppData\Local\Temp here is the code.

Python:

import tempfile
# This makes a .tmp file but only 0kb i want like 37kb
for i in range(20):
    new_file, filename = tempfile.mkstemp('.tmp')
    print(filename)

This makes a .tmp file but I want to increase the size/ storage it takes

I don't have a way for batch so please help me all support is appreciative

CodePudding user response:

The function below can be used to write (n) bytes of random data into the file specified.

Using the os.urandom function, the number of random bytes is generated, and written into the file path specified to the fpath function parameter.

Note: As this is binary data, it is not human-readable - but given it's just random data filling the file, that should not matter.

def write_temp_file(fpath: str, size: int):
    """Create a temp file filled with (n) random bytes.
    
    Args:
        fpath (str): Full path to the file.
        size (int): Number of bytes to write into the file.
    
    """
    with open(fpath, 'wb') as f:
        f.write(os.urandom(size))

Use:

>>> write_temp_file('/tmp/tempfile.tmp', 37000)

Output:

I'm using Linux, so the file display is a little bit different, but you can see a file has been written of the size 37Kb.

$ la /tmp/temp*
-rw-rw-r-- 1 user group 37K Jul 26 11:29 tempfile.tmp

Creating many .tmp files:

If you'd like to generate many .tmp file, you can wrap the function call in a loop, such as:

for i in range(1, 21):
    n = str(i).zfill(2)
    write_temp_file(f'/tmp/tempfile{n}.tmp', 37000)

Output:

$ la /tmp/temp*
-rw-rw-r-- 1 user group 37000 Jul 26 11:42 /tmp/tempfile01.tmp
-rw-rw-r-- 1 user group 37000 Jul 26 11:42 /tmp/tempfile02.tmp
...
-rw-rw-r-- 1 user group 37000 Jul 26 11:42 /tmp/tempfile19.tmp
-rw-rw-r-- 1 user group 37000 Jul 26 11:42 /tmp/tempfile20.tmp

CodePudding user response:

Whilst the question has already been answered, and a solution accepted, the OP did also include the tag.

For that reason, here is a very basic example of how to generate a dummy file of size 37KB filled with _ characters, from a batch file.

@(For /L %%G In (1,1,37) Do @For /L %%H In (1,1,1024) Do @Set /P "=_" 0<NUL) 1>"%LocalAppData%\Temp\dummy.tmp"

Just change the number of kilobytes, currently 37, (just before the first closing parenthesis), and the fill character, _, (just after the = character), as needed.

Please note that the above example will be relatively slow, so would be unsuitable for large files.

There are better ways of creating larger dummy files, one of which is often ignored, because historically it required to be 'Run as administrator'. However, certainly in Windows 10, that restriction is no longer in place for the required task, and standard users can benefit from its use. That method involves the built-in utility fsutil.exe, which has the ability to very quickly create a file of defined size, including larger ones.

Example: fsutil file createNew <filename> <length>

For a 37KB file named dummy.tmp in %SystemDrive%\Users\%UserName%\AppData\Local\Temp, (which is usually, by default, %TEMP%, and/or %TMP%), from a batch file:

@Set "sizeKB=37"
@Set /A sizeB = sizeKB * 1024
@%SystemRoot%\System32\fsutil.exe file createNew "%LocalAppData%\Temp\dummy.tmp" %sizeB%

The downside of this is that you do not have an opportunity to select the content or its format.


You could of course leverage other built-in scripting languages from a batch file, VBScript, JScript and PowerShell, but as the OP already has python.exe installed, including such things, under this question would be wasted effort.

  • Related