Resolved. Found a mistake in second file...working now as planned
if __name__=='main':
Should be
if __name__=='__main__':
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.
Resolved. Found a mistake in second file...working now as planned
if __name__=='main':
Should be
if __name__=='__main__':
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()
Thank you! Googling in all the wrong places for the past hour!...
I've installed beautiful soup, but I'm getting error messages saying I need to convert to Python 3 when it runs the beautifulSoup files. I've checked for the latest update, and tried pip3 command in staSH which gives a no command error. I did try converting the init.py file and any others that threw up an error with pythonista conversion tool to python3, but led to further problems eventually that conversion wouldn't solve.
import requests, bs4
res = requests.get('http://nostarch.com')
res.raise_for_status()
nostarchSoup= bs4.BeautifulSoup(res.text)
type(nostarchSoup)
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/B60B81CE-83F6-4C3A-867B-08E54FFE878D/Pythonista3/Documents/MyScripts/beautifulSoupTest.py", line 1, in <module>
import requests, bs4
File "/private/var/mobile/Containers/Shared/AppGroup/B60B81CE-83F6-4C3A-867B-08E54FFE878D/Pythonista3/Documents/site-packages/bs4/init.py", line 53
'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (python setup.py install
) or by running 2to3 (2to3 -w bs4
).'
^
SyntaxError: invalid syntax