Can Python ask questions and change files.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Can Python ask questions and change files.

 
0
  #1
Apr 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Can Python ask questions and change files.

 
0
  #2
Apr 21st, 2009
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:
  1. usr_inp = raw_input( 'Please enter a directory: ' )
  2. print 'The user entered:', usr_inp
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Re: Can Python ask questions and change files.

 
0
  #3
Apr 22nd, 2009
Thank you. Are there a lot of different inputs? I don't understand code tags. Do I have to wrap every line with [] or one [ at the beginning of the program, and one] at the end?
Joe
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,259
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 541
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Can Python ask questions and change files.

 
-7
  #4
Apr 22nd, 2009
Just put it like so

[code tag starts here]

CODE GOES HERE
CODE
CODE
CODE

[/code tag ends here]

put non-code stuff here
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Can Python ask questions and change files.

 
0
  #5
Apr 22nd, 2009
Originally Posted by jbennet View Post
Just put it like so

[code tag starts here]

CODE GOES HERE
CODE
CODE
CODE

[/code tag ends here]

put non-code stuff here
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!
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 14
Reputation: Mearah is an unknown quantity at this point 
Solved Threads: 0
Mearah's Avatar
Mearah Mearah is offline Offline
Newbie Poster

Re: Can Python ask questions and change files.

 
0
  #6
Apr 22nd, 2009
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:
  1. 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:

  1. filepath = raw_input("Please define the filepath")
  2. file=open(filepath)
  3. text=file.readlines()
  4. 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:
  1. import tkFileDialog
  2. file = tkFileDialog.askopenfile(filetypes=[("all formats", "*")])
  3. 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)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC