{"id":162,"date":"2011-02-13T18:02:15","date_gmt":"2011-02-13T22:02:15","guid":{"rendered":"http:\/\/blogs.tulsalabs.com\/?p=162"},"modified":"2025-11-12T02:24:50","modified_gmt":"2025-11-12T06:24:50","slug":"learning-python-mkfile","status":"publish","type":"post","link":"http:\/\/blogs.tulsalabs.com\/?p=162","title":{"rendered":"Learning Python &#8211; mkfile"},"content":{"rendered":"<p>I&#8217;m trying to learn Python, again, and the problem is in trying to code things which would be simple in C\/Perl\/etc which are hard in Python.<\/p>\n<p>I talked about how cool mkfile was in <a href=\"http:\/\/blogs.tulsalabs.com\/?p=89\">Creating a small zpool for testing<\/a> and so I thought I would try to code that up in Python. Remember, this is my first whack at Python in 2 years, so it is all pretty much fresh and I&#8217;m in a learning mode.<\/p>\n<p>I struggled with strings, bytearrays, and bufferedreaders. I still don&#8217;t know how to open a bufferedreader.<\/p>\n<p>But I finally got to a working piece of code:<\/p>\n<pre>\r\nimport array\r\n\r\ndef mkfile(file, size) :\r\n    chunk = 1024\r\n    loopto = size \/\/ chunk\r\n    filler = size % chunk\r\n\r\n    bite = bytearray(chunk)\r\n\r\n    try :\r\n        f = open(file, \"wb\")\r\n        for n in range(loopto) :\r\n            f.write(bite)\r\n\r\n        if filler > 0 :\r\n            f.write(bytearray(filler))\r\n\r\n    finally:\r\n        f.close()\r\n\r\nmkfile(\"h1024.out\", 1024)\r\nmkfile(\"h1023.out\", 1023)\r\nmkfile(\"h64.out\", 64)\r\nmkfile(\"h1025.out\", 1025)\r\nmkfile(\"h10250.out\", 10250)\r\n<\/pre>\n<p>And it actually yields appropriately sized files:<\/p>\n<pre>\r\n[thomas@snakey src]$ ls -la h*.out\r\n-rw-r--r-- 1 thomas wheel  1023 Feb 13 15:47 h1023.out\r\n-rw-r--r-- 1 thomas wheel  1024 Feb 13 15:47 h1024.out\r\n-rw-r--r-- 1 thomas wheel 10250 Feb 13 15:47 h10250.out\r\n-rw-r--r-- 1 thomas wheel  1025 Feb 13 15:47 h1025.out\r\n-rw-r--r-- 1 thomas wheel    64 Feb 13 15:47 h64.out\r\n<\/pre>\n<p>Now I need to check to see if they are all zeros:<\/p>\n<pre>\r\n[thomas@snakey src]$ dd if=\/dev\/zero of=z10250.out bs=10250 count=1\r\n1+0 records in\r\n1+0 records out\r\n10250 bytes (10 kB) copied, 4.6629e-05 s, 220 MB\/s\r\n[thomas@snakey src]$ cmp z10250.out h10250.out \r\n<\/pre>\n<p>So yeah, there are really easy ways to accomplish mkfile.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to learn Python, again, and the problem is in trying to code things which would be simple in C\/Perl\/etc which are hard in Python.&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=162"}],"version-history":[{"count":4,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":368,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions\/368"}],"wp:attachment":[{"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blogs.tulsalabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}