0

I have a piece of code to search a folder for a certain set of files, however, all the files will not always be there. I'd like to know how to let the code continue even if some files are missing.

This is my current code:

from datetime import date, timedelta
import xlrd as x
import pandas as pd
from pandas.tseries.offsets import BDay


date = date.today()
BusinessDay = (pd.to_datetime(date) - BDay(1)).strftime('%Y-%m-%d')
BusinessDay1 = (pd.to_datetime(date) - BDay(1)).strftime('%d%m%y')

# CSV FILES

# 1

TSW_Perp = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\VBA Projects\Upload File Automation\Maitland\Tshwane Perpetua ' + str(BusinessDay1) + '.csv')
TSW_Perp1 = pd.DataFrame(TSW_Perp, columns= ['Market Value'])
TSW_Perp_MV = TSW_Perp1.iat[0, 0]
TSW_Perp_DF = [['Tshwane Provident', 'Perpetua Equity Fund', 'TSHWPERPETUA', BusinessDay, 'ZAR', TSW_Perp_MV]]

# 2

TSW_Legacy = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\VBA Projects\Upload File Automation\Maitland\Tshwane Legacy Africa ' + str(BusinessDay1) + '.csv')
TSW_Legacy1 = pd.DataFrame(TSW_Legacy, columns=['TotalMarketValue'])
TSW_Legacy_MV = TSW_Legacy1.iat[0, 0]
TSW_Legacy_DF = [['Tshwane Provident', 'Legacy Africa Fund', 'TSHWLEGACYAFR', BusinessDay, 'ZAR', TSW_Legacy_MV]]

# 3
TSW_Kag = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\Python Projects\Upload File Automation\Maitland\Tshwane Kag ' + str(BusinessDay1) + '.csv')
TSW_Kag1 = pd.DataFrame(TSW_Kag, columns=['NettMarketValue'])
TSW_Kag_MV = TSW_Kag1.iat[4, 0]
TSW_Kag_DF = [['Tshw Prov', 'Kag Eq', 'TSHWANEKAGISO', BusinessDay, 'ZAR', TSW_Kag_MV]]

# 4
PSS_Sen = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\Python Projects\Upload File Automation\Maitland\PSS Sen ' + str(BusinessDay1) + '.csv')
PSS_Sen1 = pd.DataFrame(PSS_Sentio, columns=['TotalMarketValue'])
PSS_Sen_MV = PSS_Sen1.iat[0, 0]
PSS_Sentio_DF = [['Private Sector', 'Sen Eq ',
                  'PSSSEN', BusinessDay, 'ZAR', PSS_Sen_MV]]

# 5

PSS_Merg = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\Python Projects\Upload File Automation\Maitland\PSS Merg ' + str(BusinessDay1) + '.csv')
PSS_Merg1 = pd.DataFrame(PSS_Merg, columns=['Market Value'])
PSS_Merg_MV = PSS_Merg1.iat[0, 0]
PSS_Merg_DF = [['Private Sector', 'Merge',
                    'PSSPFMERGENCE', BusinessDay, 'ZAR', PSS_Merg_MV]]

# .XLS FILES

# 6

PSS_Vu = x.open_workbook(r'C:\Users\xxxx\Documents\Projects\Python Projects\Upload File Automation\Maitland\PSS Vu ' + str(BusinessDay1) + '.xls')
sheet = PSS_Vu.sheet_by_index(0)
PSS_Vu_MV = sheet.cell_value(-1, 8)
PSS_V = [["Private Sector ", "Vu", "PSSVU", BusinessDay, "ZAR", PSS_Vu_MV]]

# 7

TSW_Ses = x.open_workbook(r'C:\Users\xxxx\Documents\Projects\Python Projects\Upload File Automation\Maitland\Tshwane Ses Property ' + str(BusinessDay1) + '.xls')
sheet1 = TSW_Ses.sheet_by_index(0)
TSW_Ses_MV = sheet1.cell_value(-1, 8)
TSW_S = [["Tshw Prov", "Ses Property", "TSHPROPDOMPROPSES", BusinessDay, "ZAR", TSW_Ses_MV]]


Combined_DF = pd.DataFrame(TSW_Perp_DF + TSW_Legacy_DF + TSW_Kagiso_DF + TSW_S + PSS_Sentio_DF + PSS_V + PSS_Merg_DF, columns=['Fund', 'AssetManager', 'AssetManagerPortfolio', 'ReportDate', 'Currency', 'NettMarketValue'])
Export = Combined_DF.to_excel(r'C:\Users\SChogle\Documents\Projects\Python Projects\Upload File Automation\Maitland\Output\All Maitland Files ' + str(BusinessDay) + ".xls", index=None, header=True)

I am rather new to Python so I am unsure how to implement what I would like to do.

Any help would be much appreciated.

Saadiq
  • 101
  • 2
  • 15
  • 1
    Check with `os.path.exists` first then do the reading? – Chris Jan 21 '20 at 08:03
  • No. [Begging for forgiveness is better than asking permission](https://stackoverflow.com/questions/12265451/ask-forgiveness-not-permission-explain) – Pynchia Jan 21 '20 at 08:07

1 Answers1

2

You can use try/except to continue in case of errors:

try:
    TSW_Perp = pd.read_csv(r'C:\Users\xxxx\Documents\Projects\VBA Projects\Upload File Automation\Maitland\Tshwane Perpetua ' + str(BusinessDay1) + '.csv')
    TSW_Perp1 = pd.DataFrame(TSW_Perp, columns= ['Market Value'])
    TSW_Perp_MV = TSW_Perp1.iat[0, 0]
except:
    # This is run if an error occurs in the lines above.
    TSW_Perp_MV = "error occurred"

TSW_Perp_DF = [['Tshwane Provident', 'Perpetua Equity Fund', 'TSHWPERPETUA', BusinessDay, 'ZAR', TSW_Perp_MV]]
Sjoerd
  • 74,049
  • 16
  • 131
  • 175