0

I'm looking for fastest way to generate fake files for stress test where is a possibility to pass file size. Currently I'm using simple

with open("{}".format(i), 'wb') as f: f.write(os.urandom(FILE_SIZE))

but for my case, creating each file takes too long. It seems to me that the Faker library does not have the method to generate fake files

EDIT: The code below is just a part of whole script so any CMD/OS commands are not a solution for my problem.

  • @ Radosław Hryniewicki duplicate: https://stackoverflow.com/questions/8816059/create-file-of-particular-size-in-python – Zaraki Kenpachi May 30 '19 at 12:00
  • Not exacly. I'm looking for a faster solution to write non-spares files on disk if it exists. In the topic you mention @Shamanu4 posted answers which doesn't resolve my issue. I have to truly write sth into files with given filesize – Radosław Hryniewicki May 31 '19 at 06:01

2 Answers2

0

Wouldn't it be better to use OS commands for that?

dd if=/dev/urandom of=/tmp/x bs=1M count=1

You could start that using subprocess module:

subprocess.check_call("dd if=/dev/urandom of=/tmp/y bs=1M count=1".split(" "))
Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
  • No, because the snippet I paste is only part of the whole program. Inside my script there are a lof of conditions and exceptions which cannot be handled by simple OS command. – Radosław Hryniewicki May 30 '19 at 12:33
  • Why this command cannot be called this way `subprocess.check_call('dd if=/dev/urandom of=/tmp/y bs=1M count=1'.split(' '))`? – Marcin Pietraszek May 30 '19 at 14:11
0

You can follow the below to get command with explanation and then run the same command in for loop.

How to create a file with ANY given size in Linux?

Garry
  • 536
  • 1
  • 5
  • 11