| | |
Can Python ask questions and change files.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
Hi,
This is my first post to this forum. I am just a beginner so I am not really sure what everything in my code does. Below is my python code. I would like to change the code to ask the user where the files are located and automatically update the various programs.
Compare1.asc and Compare 2.asc are almost identical so I will only refer to Compare1.asc. These programs are used with a program called MUST. I would like to ask the user some questions and have the answers automatically update the Compare1.asc and resave. One example: The Compare1.asc below uses the directory:C:\Temp\TestPy\.
I would like to ask the user where his files are located and have the answer change the program and resave.
I am not sure if I hit on all the main points or explained my problem well enough so if you have any questions please ask.
Thanks,
Joe
Question.py--------------------------------------------------------------------------------------
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 " "
cmd = "Analysis1.bat"
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
print "Creating workbook for ANALYSIS1 run"
MUST_Rpt_File1 = 'C:\\Program Files\\PTI\\MUST 9.1\\Program\\systlist.lis'
xlsFile = 'C:\\temp\\testpy\\MyExcelFile1.xls'
xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False
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)
xlApp.ActiveWorkbook.Close(False)
xlApp.Quit()
cmd = "Analysis2.bat"
status = os.system ( "Analysis2.bat" ) #using alternate way to run MUST script
print "Creating workbook for ANALYSIS2 run"
MUST_Rpt_File2 = 'C:\\Program Files\\PTI\\MUST 9.1\\Program\\systlist.lis'
xlsFile = 'C:\\temp\\testpy\\MyExcelFile2.xls'
xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False
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)
xlApp.ActiveWorkbook.Close(False)
xlApp.Quit()
print "Finished!"
Analysis1.bat----------------------------------------------------------------------
MUST Compare1.asc
Analysis2.bat----------------------------------------------------------------------
MUST Compare2.asc
Compare1.asc---------------------------------------------------------------------
*C -
*C PSS®MUST 9.1 Response File - Recorded: Thursday, Apr 16,2009 23:00:43
*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 3
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 >> REF TO DIRECT OUTPUT <<<<<<
tran/direct/f/Case_1_systlist.lis/print syst/direct/t/
*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 0/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 2/2 2
3 0
4 0/
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/'Case1_ContSum.lis'
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
constr/print sum/print bviol/
*C >> REF TO TERMINAL OUTPUT <<<<<<
dir/t
cont/sel
All
/constraints/Branch/sel/all
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
dir/f/Case1_MonElRep/ opt/1 100/2 100//pr viol/dir/t//
save/'C:\Temp\TestPy\Compare.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
Compare2.asc--------------------------------------------------------------------
*C -
*C PSS®MUST 9.1 Response File - Recorded: Thursday, Apr 16,2009 23:00:43
*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 3
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 >> REF TO DIRECT OUTPUT <<<<<<
tran/direct/f/systlist.lis/print syst/direct/t/
*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 0/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 2/2 2
3 0
4 0/
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/'Case2_ContSum.lis'
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
constr/print sum/print bviol/
*C >> REF TO TERMINAL OUTPUT <<<<<<
dir/t
cont/sel
All
/constraints/Branch/sel/all
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
dir/f/Case2_MonElRep/ opt/1 100/2 100//pr viol/dir/t//
save/'C:\Temp\TestPy\Case2.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 my first post to this forum. I am just a beginner so I am not really sure what everything in my code does. Below is my python code. I would like to change the code to ask the user where the files are located and automatically update the various programs.
Compare1.asc and Compare 2.asc are almost identical so I will only refer to Compare1.asc. These programs are used with a program called MUST. I would like to ask the user some questions and have the answers automatically update the Compare1.asc and resave. One example: The Compare1.asc below uses the directory:C:\Temp\TestPy\.
I would like to ask the user where his files are located and have the answer change the program and resave.
I am not sure if I hit on all the main points or explained my problem well enough so if you have any questions please ask.
Thanks,
Joe
Question.py--------------------------------------------------------------------------------------
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 " "
cmd = "Analysis1.bat"
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
print "Creating workbook for ANALYSIS1 run"
MUST_Rpt_File1 = 'C:\\Program Files\\PTI\\MUST 9.1\\Program\\systlist.lis'
xlsFile = 'C:\\temp\\testpy\\MyExcelFile1.xls'
xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False
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)
xlApp.ActiveWorkbook.Close(False)
xlApp.Quit()
cmd = "Analysis2.bat"
status = os.system ( "Analysis2.bat" ) #using alternate way to run MUST script
print "Creating workbook for ANALYSIS2 run"
MUST_Rpt_File2 = 'C:\\Program Files\\PTI\\MUST 9.1\\Program\\systlist.lis'
xlsFile = 'C:\\temp\\testpy\\MyExcelFile2.xls'
xlApp = win32com.client.Dispatch( "Excel.Application" )
xlApp.Visible = False
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)
xlApp.ActiveWorkbook.Close(False)
xlApp.Quit()
print "Finished!"
Analysis1.bat----------------------------------------------------------------------
MUST Compare1.asc
Analysis2.bat----------------------------------------------------------------------
MUST Compare2.asc
Compare1.asc---------------------------------------------------------------------
*C -
*C PSS®MUST 9.1 Response File - Recorded: Thursday, Apr 16,2009 23:00:43
*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 3
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 >> REF TO DIRECT OUTPUT <<<<<<
tran/direct/f/Case_1_systlist.lis/print syst/direct/t/
*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 0/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 2/2 2
3 0
4 0/
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/'Case1_ContSum.lis'
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
constr/print sum/print bviol/
*C >> REF TO TERMINAL OUTPUT <<<<<<
dir/t
cont/sel
All
/constraints/Branch/sel/all
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
dir/f/Case1_MonElRep/ opt/1 100/2 100//pr viol/dir/t//
save/'C:\Temp\TestPy\Compare.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
Compare2.asc--------------------------------------------------------------------
*C -
*C PSS®MUST 9.1 Response File - Recorded: Thursday, Apr 16,2009 23:00:43
*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 3
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 >> REF TO DIRECT OUTPUT <<<<<<
tran/direct/f/systlist.lis/print syst/direct/t/
*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 0/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 2/2 2
3 0
4 0/
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/'Case2_ContSum.lis'
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
constr/print sum/print bviol/
*C >> REF TO TERMINAL OUTPUT <<<<<<
dir/t
cont/sel
All
/constraints/Branch/sel/all
*C >> MODIFIED REF TO EXCEL INTERFACE <<<<<<
dir/f/Case2_MonElRep/ opt/1 100/2 100//pr viol/dir/t//
save/'C:\Temp\TestPy\Case2.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
Please use code tags when posting code.
[code=python]
# Put your code inside here
[/code]
As far as Python input methods you can use raw_input to prompt the user for input:
[code=python]
# Put your code inside here
[/code]
As far as Python input methods you can use raw_input to prompt the user for input:
python Syntax (Toggle Plain Text)
usr_inp = raw_input( 'Please enter a directory: ' ) print 'The user entered:', usr_inp
This is for when you post code in the forum. It allows us other members to read your code very easily, as it doesn't come out in a jumbled mess like above. It will have nice formatting and syntax highlighting to help us solve your problems!
Hey joe,
the easiest way to ask the user something (also the path of a file) is the raw_input function.
First try this in your python-interpreter:
A little window will pop up, that contains the prompt you give as the parameter (What is your name). If you fill in your name an choose 'ok' you'll see that your input is written on the stdout. The input of this window you also can put into a variable, to use it for your things. Here is an example how you can open a file given by the user via raw_input:
you also could try that in your interpreter to see what happens. Best is you write a little textfile for this purpose.
But if we are true: everybody hates it to fill in long pathnames by hand. So we can do something more elegant. Try that in your interpreter:
And see what you get. Maybe you have to cut away then some parameters with the string-manipulation methods.
hope these little examples will bring you some light in understanding how to get a filepath from the user.
Mearah
the easiest way to ask the user something (also the path of a file) is the raw_input function.
First try this in your python-interpreter:
python Syntax (Toggle Plain Text)
raw_input("What is your name?")
A little window will pop up, that contains the prompt you give as the parameter (What is your name). If you fill in your name an choose 'ok' you'll see that your input is written on the stdout. The input of this window you also can put into a variable, to use it for your things. Here is an example how you can open a file given by the user via raw_input:
python Syntax (Toggle Plain Text)
filepath = raw_input("Please define the filepath") file=open(filepath) text=file.readlines() print text
you also could try that in your interpreter to see what happens. Best is you write a little textfile for this purpose.
But if we are true: everybody hates it to fill in long pathnames by hand. So we can do something more elegant. Try that in your interpreter:
python Syntax (Toggle Plain Text)
import tkFileDialog file = tkFileDialog.askopenfile(filetypes=[("all formats", "*")]) print file
And see what you get. Maybe you have to cut away then some parameters with the string-manipulation methods.
hope these little examples will bring you some light in understanding how to get a filepath from the user.
Mearah
When I examine myself and my methods of thougth, I come close to the conclusion that the gift of imagination has ment more to me than my talent for absorbing absolute knowledge. (A. Einstein)
![]() |
Similar Threads
- Starting Python (Python)
- Could someone please help me? (Viruses, Spyware and other Nasties)
- Combining txt from 2 files (Python)
- ftplib and the retrbinary() command (Python)
Other Threads in the Python Forum
- Previous Thread: small problem
- Next Thread: Function for text area
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied advanced application argv beginner change code color command csv def dictionary dynamic edit editing enter event examples excel file float format ftp function google gui homework import inches input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers obexftp output parameters parsing path port prime program programming projects py2exe pygame pygtk pyopengl pyqt python random recursion recursive remote return reverse scrolledtext session simple skinning smtp software sprite ssh stderr string strings strip syntax table tennis terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode unit urllib urllib2 variable voip windows wxpython






