Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- from configparser import ConfigParser
- except ImportError:
- from ConfigParser import ConfigParser
- import os
- import sys
- import shutil
- import traceback
- CONFIG_PATH = 'config.ini'
- DEFAULT_CONFIG = """\
- [OPTIONS]
- HeaderFile = header.bin
- InputFolder = input
- OutputFolder = output
- """
- def main():
- if not os.path.isfile(CONFIG_PATH):
- with open(CONFIG_PATH, 'w', encoding='utf8') as c:
- c.write(DEFAULT_CONFIG)
- config = ConfigParser()
- config.read(CONFIG_PATH)
- header_file = config.get('OPTIONS', 'HeaderFile')
- input_dir = config.get('OPTIONS', 'InputFolder')
- output_dir = config.get('OPTIONS', 'OutputFolder')
- if os.path.exists(output_dir):
- shutil.rmtree(output_dir)
- os.makedirs(input_dir, exist_ok=True)
- os.makedirs(output_dir, exist_ok=True)
- if not os.path.isfile(header_file):
- sys.stderr.write("ERROR: header file '%s' not found." % header_file)
- with open(header_file, 'wb') as h:
- sys.stderr.write(" Created empty file.\n")
- return
- with open(header_file, 'rb') as h:
- header_data = h.read()
- if len(header_data) == 0:
- sys.stderr.write("ERROR: header file is empty.")
- return
- filecount = 0
- for path, folders, files in os.walk(input_dir):
- for filename in files:
- fullpath = os.path.join(path, filename)
- relpath = os.path.relpath(fullpath, input_dir)
- outpath = os.path.join(output_dir, relpath)
- os.makedirs(os.path.dirname(outpath), exist_ok=True)
- with open(fullpath, 'rb') as i, open(outpath, 'wb') as o:
- o.write(header_data)
- o.write(i.read())
- print(relpath)
- filecount += 1
- print("Total %d files" % filecount)
- if __name__ == "__main__":
- try:
- main()
- except:
- print(traceback.format_exc())
- finally:
- os.system('pause')
Advertisement
Add Comment
Please, Sign In to add comment