简体   繁体   中英

Open pdf files format with python

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

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM