Skip to content

Instantly share code, notes, and snippets.

@sahutd
Created October 20, 2014 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahutd/fff0e78c3751d3673e41 to your computer and use it in GitHub Desktop.
Save sahutd/fff0e78c3751d3673e41 to your computer and use it in GitHub Desktop.
import os
import sys
from subprocess import Popen
def get_files(path=''):
print("## EXTRACTING FILE LIST ##")
files = []
for ext in ('.java', '.txt'):
files += [os.path.join(path, f) for f in os.listdir(path) if
f.endswith(ext)]
order = {}
for f in files:
pos = int(input("Enter relative position for " + f))
order[pos] = f
files = sorted(order.items(), key=lambda s: s[0])
return [a[1] for a in files]
def read_file(file):
print("## READING FILE {} ##".format(file))
with open(file) as f:
content = f.readlines()
return content
def write_to_temp_file():
print("## WRITING TO TEMP FILES ##")
with open('temp', 'w') as output_file:
for file in get_files(sys.argv[1]):
filename = os.path.basename(file)
print('{}'.format(filename), file=output_file)
for line in read_file(file):
if line != '\n':
print('{}'.format(line[:-1]), file=output_file)
def convert_to_ps():
proc = Popen(['enscript', '--file-align=2', '-B', '-p', 'temp.ps', 'temp'])
proc.wait()
print("## Converted to .ps ##")
def convert_to_pdf():
proc = Popen(['ps2pdf', 'temp.ps', 'print.pdf'])
proc.wait()
print("## Converted to PDF ##")
def clean_up():
proc = Popen(['rm', 'temp.ps', 'temp'])
proc.wait()
def process():
try:
write_to_temp_file()
except:
print('format: python filesToPdf.py <folder-name>\n')
sys.exit(0)
convert_to_ps()
convert_to_pdf()
clean_up()
if __name__ == '__main__':
process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment