0

I try to open 100 pdf files with python 2.7 with this code:

import arcpy,fnmatch,os

rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        os.startfile(rootPath)
        counter = counter + 1
print counter

as a result the rootPath folder opened and python print the number of pdf files:

>>> 
39
>>> 

No pdf files opened. I search in the forum and didn't find any question with answers to my request. Thanks for any help

3
  • 2
    are your sure rootPath returns you the name of the PDF file? I see you defind rootPath as D:\desktop and its not even the name of the PDF you're trying to open Commented May 12, 2015 at 7:46
  • yes i sure it return Commented May 12, 2015 at 7:50
  • Try an onteractive python shell. Copy the full path of an existing pdf file, with proper backlash escaping (i.e. D:\desktop becomes D:\\desktop) , and try: import os; os.startfile(pdf_file_path). Does Adobe reader / other PDF viewer come up? If not, the MIME type registry is suspect. Commented May 12, 2015 at 8:00

2 Answers 2

3

I don't know what are you trying to do, but os.startfile will open up adobe pdf reader (or any other reader that's set as default reader)... here how i managed to do that and it seems to be working.

import os

rootPath = "D:\\desktop"
counter = 0
for file in os.listdir(rootPath):
    if file.endswith('.pdf'):
        os.startfile("%s/%s" %(rootPath, file))
        counter = counter + 1
print counter

or without much editing your main code

import arcpy,fnmatch,os

rootPath = r"D:\desktop"
pattern = '*.pdf'
counter = 0
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        os.startfile("%s/%s" %(rootPath,filename))
        counter = counter + 1
print counter
Sign up to request clarification or add additional context in comments.

2 Comments

with second option i get error : WindowsError: [Error 2] : 'D:\\desktop/Hebrewbooks_org_19055.pdf'
@newGIS hmm still works for me without errors, probably your PDF is corrupted? you can stick with the first solution thought
0

Your are always calling

os.startfile(rootPath)

where rootPath is only "D:\desktop". You must call os.startfile with the path to the PDF file as the argument.

os.startfile("{}/{}".format(rootPath, file))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.