Managing large numbers of files often involves repetitive tasks like printing and organizing copies. This Python script automates the process by identifying files in a designated folder, sending them to the printer, copying them to a new directory, and maintaining a log of operations.
Script Overview
The script performs the following tasks:
- Identify Files for Processing: It scans a specific input folder for files that haven’t been copied yet.
- Print Files Automatically: Each file is sent directly to the printer.
- Copy Files to a Processed Folder: After printing, the file is copied to a designated folder to track its status.
- Log the Operation: Each operation is logged with a timestamp, providing a record of processed files.
Code Breakdown
1. Setting Up Paths and File Lists
The script first identifies the current script’s directory and defines paths for the input folder, processed files folder, and the log file.
import os
import sys
import shutil
from datetime import datetime
path = os.path.abspath(os.path.dirname(sys.argv[0]))
pathInput = os.path.join(path, "INPUT")
inpFiles = os.listdir(pathInput)
inpFiles = [x for x in inpFiles if not x.endswith(".zip")]
pathCopied = os.path.join(path, "KOPIER_GEDRUCKT")
inpCopied = os.listdir(pathCopied)
inpCopied = [x for x in inpCopied if not x.endswith(".zip")]
pathInput
: Directory containing files to process.pathCopied
: Directory where printed and copied files will be stored.- The script filters out any
.zip
files to only work with relevant data.
2. Reading the Log File
The script reads from a log file (log.txt
) to track which files have been processed. It ensures that the script won’t attempt to process files that have already been copied.
fnLog = os.path.join(path, "log.txt")
with open(fnLog, encoding="mac_roman", errors="ignore") as f:
inpLog = f.read().splitlines()
3. Processing Files
The script loops through each file in the input folder. If a file hasn’t already been copied, it triggers the printing and copying operations.
for workFile in inpFiles:
if workFile in inpCopied:
continue
print(f"Working for {workFile} in INPUT-folder")
src = os.path.join(path, "INPUT", workFile)
os.startfile(src, "print")
dst = os.path.join(path, "KOPIER_GEDRUCKT", workFile)
shutil.copyfile(src, dst)
- Printing:
os.startfile(src, "print")
sends the file to the default printer. - Copying:
shutil.copyfile()
copies the file to theKOPIER_GEDRUCKT
directory to ensure it isn’t processed again.
4. Logging the Processed Files
Each time a file is printed and copied, the script logs this action with a timestamp to track the operation.
ow = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
inpLog.append(f"{now} Copied and printed {workFile}")
5. Updating the Log File
Finally, the script writes the updated log back to the file, ensuring the records are persistent for the next time the script runs.
inpLog = [f"{x}\n" for x in inpLog]
with open(fnLog, "w", encoding="mac_roman", errors="ignore") as f:
f.writelines(inpLog)
Conclusion
This Python script efficiently automates a common workflow involving printing, copying, and tracking files. By eliminating the need for manual intervention, it enhances productivity and ensures a reliable process for managing important documents. This approach is particularly useful for offices or departments that handle large volumes of files and need an automated system to track their status.