0

I want to print a file first, before printing everything within a folder. I know how to import the file just to look at it.
To be more specific I am trying to print the file to my printer which is connected through Wi-Fi.

What I am asking:

  1. What code is needed to print (hard copy to printer) 1 file?

  2. What code is needed to print all files within a folder (hard copies to printer)?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0
  1. Have you tried this solution? It appears to be as easy as

    import os
    
    os.startfile("C:/Users/TestFile.txt", "print")
    

    Since I'm not on a Windows system, I can't test it, but the internet seems to agree.

  2. Once you know how to print one, printing many should not be any harder than iterating over the contents of the directory and filtering for files. For example, since you already have os imported, lets itearete over "C:/Users/data", assuming that's where you keep all your files to be printed:

    base_dir = "C:/Users/data"
    for file in os.listdir(base_dir):
      file_name = os.path.join(base_dir, file)
      if os.path.isfile(file):
        os.startfile(file, "print")
    

    You can also try using Pathlib, but for such a simple use case the advantages of it over os are not that big.

Puff
  • 503
  • 1
  • 4
  • 14
  • Ok so I tried putting in this. Import os os.startfile("C:/Users/16467/Desktop/Courseera/IBM DAta Analyst Professional Cert/Python Project For Data Science Notes/HTML Anchor Tag element.pdf" , "print") it gave me an error saying this down below: –  Dec 27 '21 at 22:24
  • Traceback (most recent call last): File "", line 1, in os.startfile("C:/Users/16467/Desktop/Courseera/IBM DAta Analyst Professional Cert/Python Project For Data Science Notes/HTML Anchor Tag element.pdf" , "print") OSError: [WinError 1155] No application is associated with the specified file for this operation: 'C:/Users/16467/Desktop/Courseera/IBM DAta Analyst Professional Cert/Python Project For Data Science Notes/HTML Anchor Tag element.pdf' –  Dec 27 '21 at 22:27
  • Alright I downloaded Adobe Acrobat DC considering it's application to look at pdfs. I didn't have it before which was making it not work. Now that I have it, it printed 1. Now I will try to print all contents within the folder. –  Dec 27 '21 at 22:50
  • I am confused on what you mean by (base_dir) can you elaborate on what that is. –  Dec 31 '21 at 03:00
  • That's just the name I gave to the variable that stores the name of the directory with all your files, should have made taht clearer. Edited! – Puff Jan 02 '22 at 15:55