Hi This is my second post. I am new to python. I hope I have posted my code correctly. I am trying to have Python rewrite the compare.asc file. This file is used with another program called MUST. This is a transmission line program. This program is suppose to compare the differences between two cases. I would like to ask the user where the files are located and the name of the files. The files of interest are the .sav, .sub,.con, .mon. The .sav case will change but the .sub, .con, .mon should not change for the comparison. I would like the default dir to be C:\Temp\TestPy, but allow the user to change this file if necessary. This program will then pull the text files back into excel and find the differences in the numbers. I have not yet finished that macro yet. I don't know how to ask the user the location and file name ( keeping the default location). Have the users answers read back into the two .asc files and save these files as .asc files before it starts to run the MUST section.
I appreciate any help I can get with this.
Thanks,
Joe

[import win32com.client
import os, os.path
#########################################################################################################
#This is the Compare.asc File
#*C -
#*C PSSĀ®MUST 9.1 Response File - Recorded: Saturday, Apr 25,2009 00:31:40
#*C -
#
#optn
#2 9999999
#3 1
#*blank
#y
#pscase
#'C:\Temp\TestPy\y08_09sR3.sav'#Dir + Case # 1
#acsolv/opt/adj/1 1/2 0/3 1/4 0/5 1/
#gen/1 1/3 0/
#sol/1 30/2 0.6/3 0/4 0/5 0.9/7 0.7/8 15///
#ACSOLV/LFN
#*if *MUST_STATUS LE 2 then
#subs/read
#'C:\Temp\TestPy\ACCC-MUST.sub' #Dir + #SUB Name
#*blank
#*endif
#
#*if *MUST_STATUS LE 2 then
#constr
#1 2.00000
#2 1
#3 100
#4 3
#5 100
#6 4
#7 0
#8 3
#9 10000
#/y
#*endif
#
#*if *MUST_STATUS LE 2 then
#read
#'C:\Temp\TestPy\OUC.mon' #Dir + #MON Name
#'C:\Temp\TestPy\FL230-FMPP.con'
#*blank
#*endif
#
#*if *MUST_STATUS LE 2 then
#constr//excl/read/ 'C:\Temp\TestPy\NonFirmExclude.exc'// #Dir + #EXC Name
#*endif
#*if *MUST_STATUS LE 2 then
#must
#1 0
#2 1
#3 50000
#4 0.03000
#5 3
#6 1
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#*endif
#*C AC MENU shifts indirectly recorded
#*C >> MENU SHIFT <<<<<<
#/3/
#MUST
#1 0
#2 1
#3 50000
#4 0.02000
#5 100
#6 0
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#*C >> MENU SHIFT <<<<<<
#/3/ACCONT
#lfopt
#adj/1 1/2 0/3 1/4 0/5 1/
#gen/1 1/3 0
#/sol/1 30/2 0.6/3 0/4 0/5 0.9/7 0.7/8 15//
#opt/1 1/2 2
#3 0
#4 5/
#exe
#*C
#*C Select all constraints
#constra
#bra/sel/all/
#int/sel/all/
#fgate/sel/all/
#*blank
#*C
#*C
#*C Select all contingencies
#contin/sel/all/
#*C
#dir/f/'ContSum.lis'
#*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
#
#constr/print sum/print bviol/
#*C >> REF TO TERMINAL OUTPUT <<<<<<
#dir/t
#cont/sel
#HSEV/100
#/constraints/Branch/sel/all
#*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
#dir/f/MonElRep/ opt/1 100/2 100//pr viol/dir/t//
#save/'C:\Temp\TestPy\Case1.acc'
#/
#/MUST
#1 0
#2 1
#3 50000
#4 0.03000
#5 3
#6 1
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#/3/
#stop
#########################################################################################################
#########################################################################################################
##This is the Compare # 2.asc File
#*C -
#*C PSSĀ®MUST 9.1 Response File - Recorded: Saturday, Apr 25,2009 00:31:40
#*C -
#
#optn
#2 9999999
#3 1
#*blank
#y
#pscase
#'C:\Temp\TestPy\y08_09sR3.sav'
#acsolv/opt/adj/1 1/2 0/3 1/4 0/5 1/
#gen/1 1/3 0/
#sol/1 30/2 0.6/3 0/4 0/5 0.9/7 0.7/8 15///
#ACSOLV/LFN
#*if *MUST_STATUS LE 2 then
#subs/read
#'C:\Temp\TestPy\ACCC-MUST.sub'
#*blank
#*endif
#
#*if *MUST_STATUS LE 2 then
#constr
#1 2.00000
#2 1
#3 100
#4 3
#5 100
#6 4
#7 0
#8 3
#9 10000
#/y
#*endif
#
#*if *MUST_STATUS LE 2 then
#read
#'C:\Temp\TestPy\OUC.mon'
#'C:\Temp\TestPy\FL230-FMPP.con'
#*blank
#*endif
#
#*if *MUST_STATUS LE 2 then
#constr//excl/read/ 'C:\Temp\TestPy\NonFirmExclude.exc'//
#*endif
#*if *MUST_STATUS LE 2 then
#must
#1 0
#2 1
#3 50000
#4 0.03000
#5 3
#6 1
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#*endif
#*C AC MENU shifts indirectly recorded
#*C >> MENU SHIFT <<<<<<
#/3/
#MUST
#1 0
#2 1
#3 50000
#4 0.02000
#5 100
#6 0
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#*C >> MENU SHIFT <<<<<<
#/3/ACCONT
#lfopt
#adj/1 1/2 0/3 1/4 0/5 1/
#gen/1 1/3 0
#/sol/1 30/2 0.6/3 0/4 0/5 0.9/7 0.7/8 15//
#opt/1 1/2 2
#3 0
#4 5/
#exe
#*C
#*C Select all constraints
#constra
#bra/sel/all/
#int/sel/all/
#fgate/sel/all/
#*blank
#*C
#*C
#*C Select all contingencies
#contin/sel/all/
#*C
#dir/f/'ContSum.lis'
#*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
#
#constr/print sum/print bviol/
#*C >> REF TO TERMINAL OUTPUT <<<<<<
#dir/t
#cont/sel
#HSEV/100
#/constraints/Branch/sel/all
#*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
#dir/f/MonElRep/ opt/1 100/2 100//pr viol/dir/t//
#save/'C:\Temp\TestPy\Case1.acc'
#/
#/MUST
#1 0
#2 1
#3 50000
#4 0.03000
#5 3
#6 1
#7 1
#8 0.00000
#9 0.00000
#10 0.00000
#/y
#/3/
#stop
###########################################################################################
#This is the Analysis1.bat File
#"C:\Program Files\PTI\MUST 9.1\Program\must.exe" Compare.asc
###########################################################################################
#This is the Analysis2.bat File
#"C:\Program Files\PTI\MUST 9.1\Program\must.exe" Compare #2.asc
###########################################################################################
#Sub Macro1()
#'
#' Macro1 Macro
#' Macro recorded 4/25/2009 by hartj432
#'
#
#'
# ChDir "C:\Temp\TestPy"
# Range("E8").Select
# Workbooks.OpenText Filename:="C:\Temp\TestPy\MonElRep.LIS", Origin:=437, _
# StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(6, _
# 1), Array(18, 1), Array(23, 1), Array(30, 1), Array(43, 1), Array(47, 1), Array(51, 1), _
# Array(55, 1), Array(63, 1), Array(72, 1), Array(82, 1), Array(89, 1), Array(94, 1), Array( _
# 102, 1), Array(114, 1), Array(121, 1), Array(126, 1), Array(138, 1), Array(142, 1)), _
# TrailingMinusNumbers:=True
# Workbooks.OpenText Filename:="C:\Temp\TestPy\systlist.lis", Origin:=437, _
# StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(4, _
# 1), Array(23, 1), Array(28, 1), Array(37, 1), Array(56, 1), Array(64, 1), Array(76, 1), _
# Array(85, 1), Array(94, 1)), TrailingMinusNumbers:=True
# Sheets("systlist").Select
# Sheets("systlist").Move After:=Workbooks("MonElRep.LIS").Sheets(1)
# Workbooks.OpenText Filename:="C:\Temp\TestPy\ContSum.lis", Origin:=437, _
# StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(6, _
# 1), Array(17, 1), Array(23, 1), Array(30, 1), Array(42, 1), Array(47, 1), Array(49, 1), _
# Array(57, 1), Array(63, 1), Array(73, 1), Array(80, 1)), TrailingMinusNumbers:=True
# Sheets("ContSum").Select
# Sheets("ContSum").Move After:=Workbooks("MonElRep.LIS").Sheets(2)
# Sheets("title").Select
# Sheets("title").Move After:=Workbooks("MonElRep.LIS").Sheets(3)
# ActiveWorkbook.SaveAs Filename:="C:\Temp\TestPy\Case # 1.xls", FileFormat:= _
# xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
# , CreateBackup:=False
# Range("E30").Select
# Windows("Book1").Activate
#End Sub
###########################################################################################
#
#Sub Alignment()
# s = WorksheetFunction.CountA(Rows("1:1")) ' section number
# cs = 12 'number of columns in each section
# Application.ScreenUpdating = False
# Application.Calculation = xlCalculationManual
# shname = ActiveSheet.Name
# Set sh = ActiveSheet
#Sheets.Add
#Set shtemp = ActiveSheet
#With shtemp
# For i = 2 To s
# j1 = (i - 2) * cs + 1 'First column of the first section
# j2 = (i - 1) * cs + 1 'First column of the second section
# .Cells.ClearContents
#
# Kontrol = True
# Do
# r = WorksheetFunction.Max(sh.Cells(65536, j1).End(xlUp).Row, sh.Cells(65536, j2).End(xlUp).Row)
# Call refreshFormulas(r, shtemp, shname, j1, j2)
# If WorksheetFunction.CountIf(shtemp.Columns("E:E"), True) > 0 Then
# rw = WorksheetFunction.Match(True, shtemp.Columns("E:E"), 0)
# If .Cells(rw, 3).Value <> .Cells(rw, 4).Value Then
# If .Cells(rw, 3).Value = "Missing" Then
# sh.Cells(rw, j2).Resize(1, cs).Insert shift:=xlDown
# ElseIf .Cells(rw, 4).Value = "Missing" Then
# sh.Cells(rw, 1).Resize(1, j2 - 1).Insert shift:=xlDown
# ElseIf .Cells(rw, 3).Value = "Blank" Then
# sh.Cells(rw, j2).Resize(1, cs).Insert shift:=xlDown
# ElseIf .Cells(rw, 4).Value = "Blank" Then
# sh.Cells(rw, 1).Resize(1, j2 - 1).Insert shift:=xlDown
# End If
# Else
# sh.Cells(rw, j2).Resize(1, cs).Insert shift:=xlDown
# sh.Cells(rw + 1, 1).Resize(1, j2 - 1).Insert shift:=xlDown
# End If
# Else
# Kontrol = False
# End If
# Loop Until Kontrol = False
# Next
# Application.DisplayAlerts = False
# .Delete
# Application.DisplayAlerts = True
#End With
# Application.Calculation = xlCalculationAutomatic
# Application.ScreenUpdating = True
#End Sub
#
#Sub refreshFormulas(r, shtemp, shname, j1, j2)
#With shtemp
# .Range("A3:A" & r).FormulaR1C1 = _
# "='" & shname & "'!RC" & j1 & "&'" & shname & "'!RC" & j1 + 1 & ""
# .Range("B3:B" & r).FormulaR1C1 = _
# "='" & shname & "'!RC" & j2 & "&'" & shname & "'!RC" & j2 + 1 & "&'" & shname & "'!RC" & j2 + 2 & "&'" & shname & "'!RC" & j2 + 3 & "&'" & shname & "'!RC" & j2 + 4 & ""
# .Range("C3:C" & r).FormulaR1C1 = "=IF(RC[-2]<>"""",IF(ISNA(MATCH(RC[-2],C[-1],0)),""Missing"",MATCH(RC[-2],C[-1],0)),""Blank"")"
# .Range("D3:D" & r).FormulaR1C1 = "=IF(RC[-2]<>"""",IF(ISNA(MATCH(RC[-2],C[-3],0)),""Missing"",MATCH(RC[-2],C[-3],0)),""Blank"")"
# .Range("E3:E" & r).FormulaR1C1 = "=IF(OR(AND(RC[-1]=""Missing"",RC[-2]<>""Blank""),AND(RC[-2]=""Missing"",RC[-1]<>""Blank"")),TRUE,IF(OR(AND(RC[-1]=""Missing"",RC[-2]=""Blank""),AND(RC[-2]=""Missing"",RC[-1]=""Blank"")),"""",IF(RC[-2]<>RC[-1],TRUE,"""")))"
# Application.Calculate
#End With
#End Sub
###########################################################################################

print "This test program requires:"
print " the use of Python2.5"
print " the use of Excel2003"
print " the directory C:\Temp\TestPy exist"
print " the file AnalysisX.asc exist and is present in directory where this is run"
print " the files Analysis1.bat & Analysis2.bat exist and is present in directory where this is run"
print " that PSS(r)MUST 9.1 by installed in the default dir C:\Program Files\Pti\MUST 9.1 "
print " "

#################################################
#
# RUN ANALYSIS1
#
# define PSS(r)MUST command
# make use of batch file to ensure python process waits for batch file end
cmd = "Analysis1.bat"

#
# run PSS(r)MUST using generated ASC file
print "Executing Cmd:",cmd # convenient way to visually verify command to be run
status = os.system ( cmd ) # cmd contains command to run MUST with ASC file
#
#################################################

#################################################
#
# PYTHON code to create a work book for ANALYSIS1 run
# Code to creates a work book, imports file (MUST report) and saves as workbook
#
# File importing uses standard Excel importing procedure
# Adjustments for data organization needs to be done depending on report
# Adjustments need to be done by the user for now
#

#import win32com.client
#import os, os.path

print "Creating workbook for Compare run"

from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
xl.Workbooks.Open('C:\\temp\\testpy\\MyExcelFile1.xls')
xl.Run('Macro1') # name of the macro
# name of PSS(r)MUST report file
###MUST_Rpt_File1 = 'C:\\temp\\testpy\\MonElRep'

# name of workbook1
xlsFile = 'C:\\temp\\testpy\\MyExcelFile1.xls'
xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False

#xlApp.Workbooks.Open(xlsFile) <--- replace with OpenText ....
#
# Example below uses report systlist.lis - Systems report as input
# This example uses a text string for input file name
#
# Workbooks.OpenText Filename:= _
# "C:\Program Files\PTI\MUST 9.1\Program\systlist.lis", Origin:=437, _
# StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
# ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
# Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
# , 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _
# TrailingMinusNumbers:=True


# MUST_Rpt_File1 = "c:\Program Files\PTI\MUST 9.1\Program\SYSTLIST.LIS"

# Workbooks.OpenText Filename:=MUST_Rpt_File1, DataType:=xlDelimited, Space:=True


#ot_Origin = 437
#ot_StartRow = 1
#ot_DataType =1
#ot_TextQualifier = 1
#ot_ConsecutiveDelimiter = 1
#ot_Tab = 1
#ot_Semicolon = 0
#ot_Comma = 0
#ot_Space = 1
#ot_Other = 0

#xlApp.Workbooks.OpenText(MUST_Rpt_File1, ot_Origin,ot_StartRow,ot_DataType,ot_TextQualifier, ot_ConsecutiveDelimiter,ot_Tab, ot_Semicolon, ot_Comma, ot_Space)

xlApp.ActiveWorkbook.SaveAs(xlsFile) # save workbook
xlApp.ActiveWorkbook.Close(False) # close workbook

xlApp.Quit() # end Excel application
#
#################################################


#################################################
#
# RUN ANALYSIS2
#
#
# define PSS(r)MUST command
#
cmd = "Analysis2.bat"

#
# run PSS(r)MUST using generated ASC file
status = os.system ( cmd ) #using alternate way to run MUST script
#
#################################################


#################################################
#
# PYTHON code to create a work book for ANALYSIS2 run
# Code to creates a work book, imports file (MUST report) and saves as workbook
#
# File importing uses standard Excel importing procedure
# Adjustments for data organization needs to be done depending on report
# Adjustments need to be done by the user for now
#

print "Creating workbook for ANALYSIS2 run"

# name of PSS(r)MUST report file
MUST_Rpt_File2 = 'C:\\temp\\testpy\\MonElRep'

# name of workbook2
xlsFile = 'C:\\temp\\testpy\\MyExcelFile2.xls'


xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False

#xlApp.Workbooks.Open(xlsFile) <--- replace with OpenText ....
#
# Example below uses report systlist.lis - Systems report as input
# This example uses a variable for input file name
#
# Workbooks.OpenText Filename:= _
# "C:\Program Files\PTI\MUST 9.1\Program\systlist.lis", Origin:=437, _
# StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
# ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=False, _
# Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3 _
# , 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _
# TrailingMinusNumbers:=True

ot_Origin = 437
ot_StartRow = 1
ot_DataType =1
ot_TextQualifier = 1
ot_ConsecutiveDelimiter = 1
ot_Tab = 1
ot_Semicolon = 0
ot_Comma = 0
ot_Space = 1
ot_Other = 0

xlApp.Workbooks.OpenText(MUST_Rpt_File2, ot_Origin,ot_StartRow,ot_DataType,ot_TextQualifier, ot_ConsecutiveDelimiter,ot_Tab, ot_Semicolon, ot_Comma, ot_Space)

xlApp.ActiveWorkbook.SaveAs(xlsFile) # save workbook
xlApp.ActiveWorkbook.Close(False) # close workbook

xlApp.Quit() # end Excel application
#
#################################################

print "Finished!"]

Recommended Answers

All 2 Replies

Good luck! This looks like a mess!

You're more likely to get help if you follow the posting guidelines for this forum, including but not limited to using code tags, showing effort, and providing simple examples of reproducible errors.

After skimming over your crazy long post I'll give you an example of asking the user for input and using it to open a file:

import os

usr_path = raw_input( 'Please provide the path of the file: ' )
if os.path.exists( usr_path ): # Check for file existing
    f = open( usr_path ) # Open for reading
    lines = f.readlines()   # Read in the contents
    f.close()                     # Close the file
else:
    print 'File does not exist'

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.