Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Importing my py file causes a list of pdfs in my working directory to be printed
-
I'm trying to import a file to reuse its functions in another file, but when I run the second file to check it was importing (it doesn't do anything yet) it prints a list of pdfs in the current directory to the console. Can't figure why, as there is nowhere I even use print in the files?
Original file combinePdfs
#! python3 #combinePdfs.py - Combines all the PDFs in the specified directory into a single PDF named dest import PyPDF2, os ROOT = '/myrootfolder/' #Get all the PDF filenames def combinePdfs(dir,dest): if dir=='': dir=ROOT #print(dir) os.chdir(dir) pdfFiles = [] for filename in os.listdir(dir): if filename.endswith('.pdf'): pdfFiles.append(filename) pdfFiles.sort() pdfWriter = PyPDF2.PdfFileWriter() # Loop through all the PDF files. for filename in pdfFiles: pdfFileObj = open(filename, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFileObj) # Loop through all the pages (except the first) and add them. for pageNum in range(1, pdfReader.numPages): pageObj = pdfReader.getPage(pageNum) pdfWriter.addPage(pageObj) # Save the resulting PDF to a file. if not(dest.endswith('.pdf')): dest+='.pdf' pdfOutput = open(dest, 'wb') pdfWriter.write(pdfOutput) pdfOutput.close() def main(): dest=input('input destination filename') dir=input('input directory where pdfs to be combined are stored - just hit enter for the default folder') if dir=='': dir=ROOT combinePdfs(dir,dest) if __name__=='__main__': main()
Second file
import combinePdfs import PyPDF2, os ROOT = '/myrootfolder/' def main(): combinePdfs(ROOT,'test.pdf') if __name__=='main': main()
-
Resolved. Found a mistake in second file...working now as planned
if __name__=='main':
Should be
if __name__=='__main__':
-
Hi @sobreacain Cool script! A few thoughts...
- dir() is a builtin function so I use
dir_path
to keep from clobbering it - Use
input(x).strip()
and anor
clause to get rid of leading and trailing whitespace and set a default if the user enters nothing - Use the
with open()
syntax so that files are automatically closed and their file handles can be freed up. - Under the wrench icon, use
Check Style
andAnalyze (pyflakes)
to catch other issues
#! python3 # combinePdfs.py - Combines all the PDFs in the specified directory into a # single PDF named dest import os import PyPDF2 ROOT = '/myrootfolder/' def combinePdfs(dir_path, dest): """Get all the PDF filenames""" dir_path = dir_path or ROOT if not dest.endswith('.pdf'): dest += '.pdf' # print(dir) os.chdir(dir_path) pdfFiles = sorted(fn for fn in os.listdir(dir_path) if fn.endswith('.pdf')) if not pdfFiles: exit('No .pdf files found in {}.'.format(dir_path)) print('Processing {} .pdf files...'.format(len(pdfFiles))) pdfWriter = PyPDF2.PdfFileWriter() # Loop through all the PDF files. for filename in pdfFiles: with open(filename, 'rb') as in_file: pdfReader = PyPDF2.PdfFileReader(in_file) # Loop through all the pages (except the first) and add them. for pageNum in range(1, pdfReader.numPages): pdfWriter.addPage(pdfReader.getPage(pageNum)) # Save the resulting PDF to a file. with open(dest, 'wb') as out_file: pdfWriter.write(out_file) def main(): dest = input('input destination filename').strip() or 'default.pdf' dir_path = input('input directory where pdfs to be combined are stored - ' 'just hit enter for the default folder').strip() or ROOT combinePdfs(dir_path, dest) if __name__ == '__main__': main()
- dir() is a builtin function so I use