forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirs.py
35 lines (31 loc) · 1.45 KB
/
dirs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
# Creates a number of dirs and a number of unique files in each dir
# Usage: testdirs.py [number of dirs] [number of files]
import sys
import os
try:
# assign the script name (argv[0]) to "scriptname"
# assign the second argument (argv[1]) to "numdirs"
# assign the third argument (argv[2]) to "numfiles"
scriptname, numdirs, numfiles = sys.argv
except ValueError:
# if the user has not passed 3 arguments, a ValueError exception is raised
# so we print the usage message and quit
print "Creates a number of dirs and a number of unique files in each dir"
print "Usage: {} [number of dirs] [number of files]".format(sys.argv[0])
sys.exit()
# for each directory number (this starts at 0, we'll add 1 later)
for dirnum in range(int(numdirs)):
# make a directory name called "testdir-X" starting at 1
dirname = "testdir-{}".format(dirnum + 1)
# make that directory
os.makedirs(dirname)
# for each file number (this starts at 0, we'll add 1 later)
for filenum in range(int(numfiles)):
# create a full filename called "testdir-X/testfile-Y" starting a file 1
filename = "{}/testfile-{}".format(dirname, filenum + 1)
with open(filename, "w") as file:
# write the full filename into the file (which is unique)
file.write(filename)
# write urandom into the file (which may not be unique)
file.write(os.urandom(512 * 5))