2017-12-04 5 views
0

私はopenpyxlを使用してExcel内のリストから特定のファイルを一括コピーするのに役立っています。フォルダからコピーし、ファイル名がリストに発見された場合、特定のファイル名の接頭辞 のためのワークシートを検索し 、および特定の接頭辞で始まります:現在続いされているプロセスがあるファイルが存在しない場合の省略方法 - openpyxl、os

#Import required modules 
from openpyxl import load_workbook 
import os 
import shutil 
# Enter source folder 

source = ("C:\\Users\\alec.litchfield\\Desktop\\Resized images\\") 
# Enter destiantion folder - this must exist. 
destination = ("C:\\Users\\alec.litchfield\\Desktop\\Stripped Blackpool Photos\\") 
# Enter the name of your Excel Workbook (including the extension): 
workBookName = ("C:\\Users\\alec.litchfield\\Desktop\\2017.11.22 Point Data - Master.xlsx") #input("Enter the name of your Excel Workbook (including the extension): ") 
workSheetName = ("Point data") #input("Enter the name of the work sheet to read from: ") 
RowRange = int(input ("add total number of rows to check: ")) 
workBook = load_workbook(filename = workBookName, read_only=True,data_only=True) 
workSheet = workBook[workSheetName] 
#filepath = ("C:\\Users\\alec.litchfield\\Desktop\\My Personal Folder\\Python lessons\\Photo copy paste\\") 
for x in range(2, RowRange): 
    file = str (workSheet["j" + str(x)].value) 
      if file.startswith(('P1010','P1020', 'P1070','P1060','P1050' 'P1040' 'P1020')): 
     shutil.copyfile (source + file, destination + file) 

私は取得していますこの問題を、ということですファイルが存在しない場合、プロセスは停止します。これは停止を引き起こすために1つしかかからず、コピーしているファイルの数を扱うときに問題があります(4000)

ファイルが存在しない場合は、無視して移動する '

事前に感謝します!

アレック

+0

'os.path.isfile(workBookName)' –

答えて

0

例えば(最終的には、除いて、試してください)

をPythonの例外機能をチェックアウト:

try: 
    #do stuff with the file here 
except FileNotFoundError: 
    #do nothing if the file is not found 
    pass 
関連する問題