diff options
author | rsiddharth <rsiddharth@ninthfloor.org> | 2014-10-29 23:24:58 -0400 |
---|---|---|
committer | rsiddharth <rsiddharth@ninthfloor.org> | 2014-10-29 23:24:58 -0400 |
commit | 2def977472b2e77ee88c9177f2d03f12b0263eb0 (patch) | |
tree | 2080d8304afb0b242b3251ee71de1863011a41fd /combox/utils/file.py |
Initial commit: File splitter & File gluer done.
new file: .gitignore
new file: AUTHORS
new file: COPYING
new file: combox/__init__.py
new file: combox/tests/__init__.py
new file: combox/tests/files/the-red-star.jpg
new file: combox/tests/utils_file.py
new file: combox/utils/__init__.py
new file: combox/utils/file.py
I like to write python FWIW. But after reading a dialect of Lisp when
I come back to python, it does not look very beautiful. I guess I'm
pretty convinced that there is no language that can ape the beauty of
Lisp.
Diffstat (limited to 'combox/utils/file.py')
-rw-r--r-- | combox/utils/file.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/combox/utils/file.py b/combox/utils/file.py new file mode 100644 index 0000000..bb27592 --- /dev/null +++ b/combox/utils/file.py @@ -0,0 +1,84 @@ +# Copyright (C) 2014 Combox author(s). See AUTHORS. +# +# This file is part of Combox. +# +# Combox is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Combox is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Combox (see COPYING). If not, see +# <http://www.gnu.org/licenses/>. + +from os import path +from sys import exit + + +def split_file(filename, n): + """Split the file into `n' parts and return them as an array. + + filename: Absolute pathname of the file. + n: Number of parts the file has to be split. + """ + + file_ = None + try: + file_ = open(filename, 'rb') + except IOError: + print "ERROR: opening %s" % (filename) + exit(1) + + f_parts = [] + # File size in bytes. + file_size = path.getsize(filename) + # No. of bytes for each file part. + part_size = file_size / n + # Take note of remaining bytes; this is non-zero when file_size is + # not divisible by `n'. + rem_bytes = file_size % n + + i = 0 + while i < n: + f_parts.append(file_.read(part_size)) + i += 1 + + # read the remaining bytes into the last file part. + f_parts[n-1] += file_.read(rem_bytes) + + return f_parts + + +def glue_file(f_parts): + """Glue different parts of the file to one. + + f_parts: Array containing different parts of the file. Each part + is a sequence of bytes. + """ + + file_content = '' + for part in f_parts: + file_content += part + + return file_content + + +def write_file(filename, filecontent): + """Write `filecontent' to `filename'. + + filename: Absolute pathname of the file. + filecontent: String/bytstream to write to filename. + """ + + file_ = None + try: + file_ = open(filename, 'wb') + file_.write(filecontent) + except IOError: + print "ERROR: creating and writing content to %s" % (filename) + exit(1) |