0

I have created a script which includes the below.

The idea is to go through a list of excel files (Countries) in a folder and then, if it exists already to pass through and ignore, if it does not exist then to create it. In both cases, it will then move on to the rest of the script. I want to create it this way because I do not want to overwrite the other countries files every time as there will be data dating back a few months in each file (so overwriting will lose this). I would like to just create a new file for when one new country gets added to the list.

I cannot just create a file manually, as the rest of the script takes into account all files.

if ''.join(Countries) in path:
    pass
else:
    for x in Countries:
        workbook = xlsxwriter.Workbook(path + x)
        worksheet = workbook.add_worksheet(sheetname)

        FirstSheet = ([firstMes])

        row = 0
        col = 0

        for y in (FirstSheet):
            worksheet.write(row, col, y)
            row += 1

        workbook.close()
thrnma
  • 41
  • 5
  • I don't understand the `if` statement. What is the value of `path`, and why would it contain the concatenation of all the countries? – Barmar May 22 '20 at 14:25
  • You probably should be checking `if os.path.exists(os.path.join(path, x)):` – Barmar May 22 '20 at 14:26
  • @Barmar - using if os.path.exists(os.path.join(path, x)): works! I also put it inside the loop, instead of outside the loop. Thank you for your help :) – thrnma May 22 '20 at 14:41

0 Answers0