Home > other >  python ShellExecute cannot find files on a shared folder on another computer
python ShellExecute cannot find files on a shared folder on another computer

Time:01-28

Recently I use modules win32print and win32api to accomplish batch printing PDF files by python. When I select pdf files on my local computer, it did well. However, when I select pdf files on a shared folder on another computer(the folder path like "\\filepath"), The problem appeared, which showed "pywintypes. error: (2, 'ShellExecute', 'The system cannot find the file specified)". I do not know why.

error

# Batch print pdf files

import win32print
import win32api
import os

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
 
def print_file(filename):
    open(filename,"r")
    win32api.ShellExecute(
        0,
        "print",
        filename,
        '/d:"%s"' % win32print.GetDefaultPrinter(),
        ".",
        0
    )
    
root = tk.Tk()
root.withdraw()

files_path = filedialog.askopenfilenames()
num = len(files_path)

if num == 0:
    messagebox.showinfo("Prompting","No files selected!")
else:
    msg = messagebox.askyesno('Prompting', 'Pring'  str(num) "files?")
    
    if msg:
        i = 0
        for file_path in files_path:
            if file_path.endswith("pdf"):
                print_file(file_path)
                i = i   1
        messagebox.showinfo("Prompting","Done!\n"   "Totally printing" str(i) "files!")
    else:
        messagebox.showinfo("Prompting","Nothing done!")

CodePudding user response:

probably you can use these:

import glob
path = r'./folder/folder/'
all_files = glob.glob(path   "/*.pdf")
files = all_files[:-1]
print("number of pdf files: ", len(files))
for filename in files:
   //open file

CodePudding user response:

on another computer(the folder path like "\\filepath"), The problem appeared, which showed "pywintypes. error: (2, 'ShellExecute', 'The system cannot find the file specified)". I do not know why.

The answer is that same as posted above in your question where you added one extra, you need to use "\\\\server\\filepath" using \ is considered an escape so each needs to be doubled. A lan file share is \\served/path/filename unless its mapped to a drive letter:/path/filename

  •  Tags:  
  • Related