Printing files within a folder using python

Viewed 17

I am trying to improve a code I found online so that it doesn't waste so much paper.

The files I will be printing are small labels which do not take a lot of space. I would like to be able to "merge" those labels so that they'd be printed out by groups of 6 per page.

This is the code (it already has some modifications):

# Import libraries
import os
from pickle import TRUE
import time
import tkinter as tk
from tkinter import filedialog
 
def menu():
    print("1- Retry")
    print("0- Exit")
    s=TRUE
    while s: 
        opt = input("Select an option: ")
        try:
            if int(opt) == 1 or int(opt) == 0:
                opt = int(opt)
                s = False
        except:
            input("Press enter to try again.")
    return opt

s = TRUE
while s:
    root = tk.Tk()
    root.withdraw()
    path = filedialog.askdirectory()
    try:    
    # Extracting all the contents in the directory corresponding to path
        l_files = os.listdir(path)
        
        # Iterating over all the files
        for file in l_files:
        
        # Instantiating the path of the file
            file_path = f'{path}\\{file}'
        
            # Checking whether the given file is a directory or not
            if os.path.isfile(file_path):
                try:
                    # Printing the file pertaining to file_path
                    os.startfile(file_path, 'print')
                    print(f'Printing {file}')
        
                    # Sleeping the program for 5 seconds so as to account the
            # steady processing of the print operation.
                    time.sleep(5)
                except:
                    # Catching if any error occurs and alerting the user
                    print(f'ALERT: {file} could not be printed! Please check\
                    the associated softwares, or the file type.')
            else:
                print(f'ALERT: {file} is not a file, so can not be printed!')
            s = False
    except:
        print('No folder selected.')
        opt = menu()
        if opt == 0:
            break
        else:
            pass    
print('Task finished!')
0 Answers
Related