By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

Giving the input file name from the prompt

100+
P: 440
HI,

I have a Samplescript.When I run this Script,it should pop up message saying that whether to run Batch or Interactive

I/P:
SampleScript.py or SampleScript.pyc

>>> SampleScript.py --> Run the script from the prompt
>>> 'Message-Do you want to run as Interactive' --> Program will prompt the message
>>> Yes ---> ( user will type 'Yes' or 'No')
>>> SampleData.txt ---> ( Text file is given as a input)
Application start running

(or )

>>> No
Graphical user interface is pop up
User will input the data
Application start running

Could anybody hlep me in providing the sample code for this

Thanks
PSB
May 21 '07 #1
Share this Question
Share on Google+
17 Replies


100+
P: 440
How to get the inputs from the prompt?

Like filename or any flags


-PSB
May 21 '07 #2

bartonc
Expert 5K+
P: 6,596
How to get the inputs from the prompt?

Like filename or any flags


-PSB
I don't think that there is a built-in way to "pop up" the standard python shell. Many good toolkits (like wxPython) have shell re-direction in a window that can be controled by the app. In either case:
Expand|Select|Wrap|Line Numbers
  1. usersChoice = raw_input("Please enter your choice: ")
prints a prompt and waits for charaters from the user followed be the return key. At which point it returns a string.
May 21 '07 #3

100+
P: 440
I don't think that there is a built-in way to "pop up" the standard python shell. Many good toolkits (like wxPython) have shell re-direction in a window that can be controled by the app. In either case:
Expand|Select|Wrap|Line Numbers
  1. usersChoice = raw_input("Please enter your choice: ")
prints a prompt and waits for charaters from the user followed be the return key. At which point it returns a string.
So we cannot type from the command line argumnets as mentioned below

Say

C:> SampleScript.py
C:> Yes
c:> SampleInput.txt

Similarly in UNIX

/usr/test/> SampleScript.py
/usr/test/> Yes
/usr/test/>SampleInput.txt

Thanks
PSB
May 21 '07 #4

bartonc
Expert 5K+
P: 6,596
So we cannot type from the command line argumnets as mentioned below

Say

C:> SampleScript.py
C:> Yes
c:> SampleInput.txt

Similarly in UNIX

/usr/test/> SampleScript.py
/usr/test/> Yes
/usr/test/>SampleInput.txt

Thanks
PSB
Oh, but you can.
Expand|Select|Wrap|Line Numbers
  1. print "Starting application"
  2. usersChoice = ""
  3. while usersChoice.lower() not in ("yes", "no"):
  4.     usersChoice = raw_input("Is your choice 'yes' or 'no'?")
  5.  
  6. print usersChoice
  7.  
D:\My Documents\Python\TheScripts examples>test21.py
Starting application
Is your choice 'yes' or 'no'?help
Is your choice 'yes' or 'no'?no
no
D:\My Documents\Python\TheScripts examples>
May 21 '07 #5

bartonc
Expert 5K+
P: 6,596
Oh, but you can.
Expand|Select|Wrap|Line Numbers
  1. print "Starting application"
  2. usersChoice = ""
  3. while usersChoice.lower() not in ("yes", "no"):
  4.     usersChoice = raw_input("Is your choice 'yes' or 'no'?")
  5.  
  6. print usersChoice
  7.  
D:\My Documents\Python\TheScripts examples>test21.py
Starting application
Is your choice 'yes' or 'no'?help
Is your choice 'yes' or 'no'?no
no
D:\My Documents\Python\TheScripts examples>
Or better, still:
Expand|Select|Wrap|Line Numbers
  1. print "Starting application"
  2. usersChoice = ""
  3. while usersChoice not in ("y", "n"):
  4.     usersChoice = raw_input("Is your choice 'yes' or 'no'? ")[0:1].lower() # slice prevents index errors
  5.     print repr(usersChoice)
  6. if usersChoice == 'y':
  7.     print "The reply was affirmative"
  8.  
D:\My Documents\Python\TheScripts examples>test21.py
Starting application
Is your choice 'yes' or 'no'? Yelp
'y'
The reply was affirmative
D:\My Documents\Python\TheScripts examples>
May 21 '07 #6

100+
P: 440
Oh, but you can.
Expand|Select|Wrap|Line Numbers
  1. print "Starting application"
  2. usersChoice = ""
  3. while usersChoice.lower() not in ("yes", "no"):
  4.     usersChoice = raw_input("Is your choice 'yes' or 'no'?")
  5.  
  6. print usersChoice
  7.  
D:\My Documents\Python\TheScripts examples>test21.py
Starting application
Is your choice 'yes' or 'no'?help
Is your choice 'yes' or 'no'?no
no
D:\My Documents\Python\TheScripts examples>
I hope this will work in UNIX & Windows OS without Tkinter module installed
May 23 '07 #7

bartonc
Expert 5K+
P: 6,596
I hope this will work in UNIX & Windows OS without Tkinter module installed
Of course it will; it's pure python. Notice that there are no import statements.
May 23 '07 #8

100+
P: 440
I am looking for the input file to be taken from the cmd line as shown below

Sample.py
----------------
After creating a 'Exe' of an application.I will run the application thru command prompt.The exe has to take the input data from the command prompt (i.e sample.txt)

C:\> Sample.exe sample.txt

And the application will start processing

(or )

From 'pyc'

C:\> Sample.pyc sample.txt

(or)

C:\> Sample.py sample.txt

This can be do able in shell scripting

Has anybody tried on this in Python.So that I would like to know ,how it can be implemented in Python.

Thanks
PSB
May 23 '07 #9

100+
P: 440
Help!!!

Any solution for this requirement .

-PSB
May 24 '07 #10

bartonc
Expert 5K+
P: 6,596
I am looking for the input file to be taken from the cmd line as shown below

Sample.py
----------------
After creating a 'Exe' of an application.I will run the application thru command prompt.The exe has to take the input data from the command prompt (i.e sample.txt)

C:\> Sample.exe sample.txt

And the application will start processing

(or )

From 'pyc'

C:\> Sample.pyc sample.txt

(or)

C:\> Sample.py sample.txt

This can be do able in shell scripting

Has anybody tried on this in Python.So that I would like to know ,how it can be implemented in Python.

Thanks
PSB
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. print "starting module"
  4. print sys.argv
  5. print "ending module"
  6.  
And the command line and output:
D:\My Documents\Python\TheScripts examples>module1.py hello world
starting module
['D:\\My Documents\\Python\\TheScripts examples\\module1.py', 'hello', 'world']
ending module
May 24 '07 #11

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. print "starting module"
  4. print sys.argv
  5. print "ending module"
  6.  
And the command line and output:
D:\My Documents\Python\TheScripts examples>module1.py hello world
starting module
['D:\\My Documents\\Python\\TheScripts examples\\module1.py', 'hello', 'world']
ending module
Of course, for this to work with .exe the program will need to be in the PATH environment variable (just like python is in the PATH).
May 24 '07 #12

100+
P: 440
Of course, for this to work with .exe the program will need to be in the PATH environment variable (just like python is in the PATH).
Thanks Barton,

Here is the sample code.

Expand|Select|Wrap|Line Numbers
  1. Main.py
  2. #command line arguments
  3. # This is only smaple, validation of data has to be performed 
  4. import sys
  5.  
  6. from Batch import Batch
  7. from Interactive import Interactive
  8.  
  9. #arguments
  10. if len(sys.argv)== 1:
  11.     print 'Interactive'
  12.     objInteractive = Interactive()
  13.     objInteractive.call_interactive()
  14. elif len(sys.argv)== 2:
  15.     print 'Batch'    
  16.     option = sys.argv[1]
  17.     strInputFileData = option
  18.     objBatch = Batch()
  19.     objBatch.call_batch(strInputFileData)
  20.  
Expand|Select|Wrap|Line Numbers
  1. Batch.py
  2.  
  3. class Batch:
  4.     def call_batch(self,strFileName):
  5.         print ' Iam in batch'
  6.         print strFileName
  7.  
  8.  
Expand|Select|Wrap|Line Numbers
  1. Interactive.py
  2.  
  3. from Tkinter import Tk, Label
  4. from tkFileDialog import askopenfilename
  5. from SimpleDialog import SimpleDialog 
  6.  
  7. class Interactive:
  8.     def call_interactive(self):    
  9.         root=Tk()
  10.         l = Label(root, text="Starting Application")
  11.         l.pack()
  12.         l.config(text="Running Full GUI")
  13.         root.mainloop()       
  14.  
C:>Mainpy

Interactive

C:>Mainpy C:Sample.txt

Batch

Thanks
PSB
May 24 '07 #13

bartonc
Expert 5K+
P: 6,596
Of course, for this to work with .exe the program will need to be in the PATH environment variable (just like python is in the PATH).
I guess that you got in so deep with all that Tkinter stuff, you missed a lot of the basics.
May 24 '07 #14

100+
P: 440
I guess that you got in so deep with all that Tkinter stuff, you missed a lot of the basics.
Amazing!!!.

Without the application in the PATH ,it is working fine for me.I dont know,How it is working?.

I will try to investigate on this.
May 25 '07 #15

bartonc
Expert 5K+
P: 6,596
Amazing!!!.

Without the application in the PATH ,it is working fine for me.I dont know,How it is working?.

I will try to investigate on this.
Perhaps the command-line points to the directory of the .exe. That would do it. But that is not likely to be the case in "the real world".
May 25 '07 #16

100+
P: 440
The batch and interactive execution of the application on DOS prompt is not working consistantly.Some times it work and other times it will not work.It looks like the program thread is stopping to execute the application to run next time.

How to kill the application thread which is running already or How to identify the application is running or not?.

-PSB
May 31 '07 #17

bartonc
Expert 5K+
P: 6,596
The batch and interactive execution of the application on DOS prompt is not working consistantly.Some times it work and other times it will not work.It looks like the program thread is stopping to execute the application to run next time.

How to kill the application thread which is running already or How to identify the application is running or not?.

-PSB
I have posted an example class with very strick thread controls include
Expand|Select|Wrap|Line Numbers
  1. self.workThread.isAlive()
here.
May 31 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.