How to I do this in Python ?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Aug 16 06:59:25 EDT 2013
Hi Ganesh, and welcome!
Unfortunately, you ask your questions in reverse order. The most general
(and important) question comes last, and the least important first, so
I'm going to slice-and-dice your post and answer from most general to
least.
On Fri, 16 Aug 2013 11:51:32 +0530, Ganesh Pal wrote:
> My goal is to create various kinds of files like sparse, regular
> ,directories, hard and symlinks etc
> what would be the best way to do achieve this ?
Use your shell, such as bash or csh or equivalent. For simple tasks like
that, it will solve the problem much more simply than Python.
There are three good reasons for doing this in Python:
- "This is only a small part of a larger Python application."
- "I'm doing this to learn how to use Python."
- "I really hate my shell."
But of you just want to get the job done, and don't care what language
you use, use the shell.
Now, having said that, I'm going to assume you have a good reason to use
Python:
> Case (2) :
>
> Is there a better way to create the files in Python other than using
> sub process module and running dd command as shown below ..
>
> Example :
>
> # creating sparse File
> sparse_path = os.path.join(path,'sparsefiles')
> os.makedirs(sparse_path)
> os.chdir(sparse_path)
> sparsefiles = "dd if=/dev/zero of=sp1 count=0 bs=1 seek=10G"
> process_0 = subprocess.Popen(sparsefiles, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE, shell=True)
>
> # Creating Regular files
> Regular_path = os.path.join(path,'regularfiles')
> os.makedirs(Regular_path)
> os.chdir(Regular_path)
> regularfiles = " dd if=/dev/urandom of=file1 count=0 bs=1 seek=10"
> process_1 = subprocess.Popen(regularfiles, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE, shell=True)
What do you mean by "better"? There's probably nothing that will be
faster at rapidly copying bytes from one file to another than dd. But not
with a blocksize of 1 byte at a time. It's more usual to set bs=512 or
bs=1024.
Oh, I see you're not actually writing anything to the file (count=0). In
that case, instead of using dd, you should use touch.
I'm not sure that shell=True is a good idea.
In Python, to create a new empty file, setting its contents to empty if
it already exists:
open("filename", "w").close()
That will open the file, creating it if it doesn't exist, emptying it if
it does, then close it.
To touch a file without emptying it:
open("filename", "a").close()
To make a sparse file, assuming your file system supports it, I believe
you actually have to write at least one byte to the file:
f = open('foo', 'w')
f.seek(10000)
f.write('\0')
f.close()
[...]
> How do I loop my script to create 100 of files like sp1 , sp2 ,sp3,..
> sp100 .. using the same syntax
To generate the various file names, you need to loop over a counter from
1 to 100, and stick the count into a string. Use a for-loop and the range
function:
for i in range(1, 101):
filename = "sp" + str(i)
open(filename, "a").close()
If you are a C programmer, you might prefer this style:
filename = "sp%d" % i
Or a more object-oriented style:
filename = "sp{:d}".format(i)
--
Steven
More information about the Python-list
mailing list