0

I'm trying to write a python script which will traverse through a directory recursively and create files of different sizes inside all of them. So far I have reached here. Well I haven't written any mechanism for creating files with different sizes but I need it. I very well know there's something wrong with the file creation logic I have written. Any help is highly appreciable.

My code:

#!/usr/bin/python

import os
import uuid

for dirs in os.walk('/home/zarvis'):
  print dirs
  filename = str(uuid.uuid4())
  size = 1000000
  with open(filename, "wb") as f:
    f.write(" " * size)
jagatjyoti
  • 699
  • 3
  • 10
  • 29
  • http://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits-in-python Here you find how to generate random strings. By randomize the range you can create random files. https://docs.python.org/2/library/random.html I think you can randomize what you want with this class. – Kevin Kendzia Nov 22 '16 at 11:03
  • Randomizing it is working but I'm unable to create files recursively inside all directories. The problem lies there. – jagatjyoti Nov 22 '16 at 11:05

3 Answers3

1

Don't use a fixed size. Use random.randint to create a random size. Use os.path.join to build the full path to a file.

import os
import uuid
import random

for dirs in os.walk("/home/zarvis"):
    d = dirs[0]
    filename = str(uuid.uuid4())
    size = random.randint(1, 100)
    with open(os.path.join(d, filename), "w") as f:
        f.write(" " * size)
  • Using my answer to make your working is not really fair... ;) – Riccardo Petraglia Nov 22 '16 at 11:24
  • 2
    This is the problem of what stackoverflow is becoming... people do not want to learn... just working code... So... who cares about explanation... this is working just copy/paste... ;) – Riccardo Petraglia Nov 22 '16 at 11:26
  • @RiccardoPetraglia I completely agree. And even with a language like Python that has excellent documentation and all the tools you need to interactively find out what code does, people are lazy. But has SO ever been any better? –  Nov 22 '16 at 11:29
  • If you completely agree... then explain the answer instead of simply hunting for reputation.... ;) – Riccardo Petraglia Nov 22 '16 at 11:34
  • Good point. A first step to write a good answer to a Python question is to link to the documentation. –  Nov 22 '16 at 11:39
0

You are using os.walk in the wrong way: with os.walk you actually do not change the directory where you are working on. Your script will create files only in the directory where you run it. I do not know exactly what uuid.uuid4() does, but probably a solution could be to replace filename with

filename = os.path.join(dirs[0], str(uuid.uuid4())

You need the [0] because os.walk return a list where the first argument is the current directory where the command is "walking". Try to print the output of os.walk once to get a feeling with it.

EDIT:

Replaced the + with os.path.join after comment from @LutzHorn

Riccardo Petraglia
  • 1,943
  • 1
  • 13
  • 25
0

Try this

import os,uuid,random
for root, dirs, files in os.walk("/home/zarvis/"):
    filename = str(uuid.uuid4())
    size = random.randint(1, 1000000)
    with open(os.path.join(root, filename), "wb") as f:
        f.write(b" " * size)
Saquib Sheikh
  • 189
  • 3
  • 13