1

I am working on a directory code, where if the file exists in the path specified, it would run a macro on excel. For my else, I want to make it so if the path is not there, the file will be saved to the F drive from the master copy from the I drive and then run the code. How can I do that?

import os
import os.path

PATH='F:\Ten Year Load Forecasts 2017-2026.xlsm'
Master_Copy='I:\Ten Year Load Forecasts 2017-2026.xlsm'

if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
    print "File exists and is readable"
    xlApp = win32com.client.DispatchEx('Excel.Application') # Running Excel
    xlsPath = os.path.expanduser('F:\Ten Year Load Forecats\Ten Year Load Forecasts 2017-2026.xlsm)# Reading xlsm file
    wb = xlApp.Workbooks.Open(Filename=xlsPath) # Opening file
    xlApp.Run('csvfile3')# Running macro---- csvfile3 is the macro name. It is under the "csv" module in the VBA editor
    wb.Save()
    xlApp.Quit()

else:
    "Save Master_Copy to Path"
    "Repeat Function From above with running macro"                         
Danny
  • 147
  • 1
  • 9
  • 1
    So you're asking "How do I copy a file?"? Take a look at [How do I copy a file in python?](https://stackoverflow.com/q/123198/953482) – Kevin Sep 29 '17 at 12:40
  • I tried using that method, but I always received Permission Denied Message – Danny Sep 29 '17 at 12:47
  • 1
    make sure you're using raw string or this code will break at some point because of escape sequences: https://stackoverflow.com/questions/46488344/cmd-create-a-new-folder-but-the-filename-directory-or-volume-syntax-is-incorr/46488569?noredirect=1#comment79932335_46488569 – Jean-François Fabre Sep 29 '17 at 12:50
  • 1
    @Danny, OK then. Perhaps this will be useful: [Python. IOError: \[Errno 13\] Permission denied: when i'm copying file](https://stackoverflow.com/q/7518067/953482) – Kevin Sep 29 '17 at 12:54

0 Answers0