Helll I am trying to figure out how to enter arguments throught he command line for a program I have created that is already working. I just want to adapt it to work at the command line. Here is the code. - import random
-
-
print\
-
"""
-
#---------------#
-
# RPG 1.0 #
-
#---------------#
-
-
Welcome to Random_Point_Generator 1.0 (RPG 1.0),RPG is very simple to use.
-
RPG generates a series of random points and writes these points to a .xyz
-
file. In order to generate these points RPG asks the user to input two sets
-
of coordinates and the number of points the user wishs to generate.RPG first
-
asks for an upper_left and a lower_right set of coordinates.It will then ask
-
the user for the number of points that the they wishs it to generate.
-
Random points will be generated within the bounds of the upper_left and
-
lower_right coordinate values.The program terminates when the user has
-
finished entering their xLR and yLR values.
-
"""
-
-
-
-
print "\nPlease enter the upper left coordinates\n"
-
xUL = float(raw_input("enter your x value: "))
-
yUL = float(raw_input("enter you y value: "))
-
-
print "\nPlease enter the lower right coordinates\n"
-
xLR = float(raw_input("enter your x value: "))
-
yLR = float(raw_input("enter your y value: "))
-
-
-
points = int(raw_input("\n\nHow many points do you want generated? "))
-
-
f = open(r'points.xyz', 'w')
-
-
-
f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
-
-
-
f.close()
-
-
print '\nThanks for using RPG 1.0, your "points.xyz" file should now have been created.'
-
-
raw_input("\nPress Enter to exit.")
-
-
I would like to make it so that the user is never asked to enter anything just so that they can enter arguments from the c:\ for example the entering of carguments would look like this
c:\2535358.68,2 3689.36,1423.35 ,8236.32,2000,p oints.txt
basically in the above there are six arguments entered, X and Y coordinates for the upper left value, X and Y coordinates for the lower right value, the 2000 is for the amount of pionts the user wishs to generate and the points.txt is the file they wisht to be created where these points will be stored? I have looked at some command line parsing but it's rather technical. Any help would be great thanks.
5 1763
what you need is the sys module. example -
import sys
-
arguments = sys.argv[1:] #get all arguments
-
...
-
..
-
what you need is the sys module. example -
import sys
-
arguments = sys.argv[1:] #get all arguments
-
...
-
..
-
Thanks I should have been more specific, I have looked at the sys module, but how do I specify the arguments and how do I then run the program at the command line ?
dshimer 136
Recognized Expert New Member
I just use - python progname.py arg1 arg2
remember that argv[0] is the program name itself, for example - C:\tmp>cat testargv.py
-
import sys
-
print sys.argv
-
-
C:\tmp>python testargv.py a 2 III
-
['testargv.py', 'a', '2', 'III']
I just use - python progname.py arg1 arg2
remember that argv[0] is the program name itself, for example - C:\tmp>cat testargv.py
-
import sys
-
print sys.argv
-
-
C:\tmp>python testargv.py a 2 III
-
['testargv.py', 'a', '2', 'III']
Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ? - import sys
-
-
-
-
def arguments(x1,y1,x2,y2,points,filename):
-
if __name__ == '__main__':
-
import sys
-
try:
-
arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
-
except (IndexError, TypeError):
-
print "Usage: Too few arguments"
-
-
-
f = open(r'points.xyz', 'w')
-
-
f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
-
-
f.close()
-
-
raw_input("\nFinished, Press enter to quit.")
-
bartonc 6,596
Recognized Expert Expert
Can anyone tell me why this code I have made depicted just below doesn't work... I can't figure it out ? - import sys
-
-
-
-
def arguments(x1,y1,x2,y2,points,filename):
-
if __name__ == '__main__':
-
import sys
-
try:
-
arguments(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]), int(sys.argv[5]),sys.argv[6])
-
except (IndexError, TypeError):
-
print "Usage: Too few arguments"
-
-
-
f = open(r'points.xyz', 'w')
-
-
f.writelines(['%9.2f%14.2f\n' % (random.uniform(xUL,xLR),random.uniform(yLR,yUL))for _ in range(points)])
-
-
f.close()
-
-
raw_input("\nFinished, Press enter to quit.")
-
Use the following general format - def aFunction(arg):
-
pass
-
if __name__ == '__main__':
-
import sys
-
# try
-
# convert sys.argvs
-
# except
-
# whatever
-
aFuncion(arg)
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Eric Chong |
last post by:
I created a Windows Service in C# that requires to get
passed command arguments like a Console App. I noticed
that there is an option "Start parameters" text box in
the property of a Windows Service in MMC. Is there any
way to get arguments using this option? If possible, what
framework method should I use to get an arguement?
Thanks in advance.
Eric
|
by: TEK |
last post by:
Hello
I'm wondering if anyone out there might give some
input/suggestions/viewpoints around the Command pattern.
In my case, the number one priority for using the pattern is undo support.
Some now, a lot more in the future.
I think I have a pretty good understanding about how the command pattern is
tought to work, however during implementation I'm htting a couple of quite
large design considerations that I'm not sure I have the answare...
|
by: m7b52000 |
last post by:
Some time ago I wrote a little program in Tcl/Tk that took the values
from 3 sliders and performed a calculation using these values. The
calculation was of course automatically repeated each time a slider was
moved.
It is proving most difficult in Python. How do I pass the .get() values
to my calculating function? Do I use the command option for each
slider? e.g command = Calc(a.get()). Obviously not cos it doesn't work.
I am not a...
|
by: Phoe6 |
last post by:
The Program Fragment is this:
int choice; /* Users Input Command */
..
..
..
printf("Enter a command: ");
fflush(stdin);
choice = getc(stdin);
printf("\n");
|
by: PAPutzback |
last post by:
The process and execute methods want a path to the executable otherwise
they kick out a file not found. So how can I execute the following. It
works fine from a command window.
echo password| gpg.exe --yes --output c:\working\inbound\test.txt
--passphrase 0 -d c:\working\inbound\anstest.gpg
| |
by: Steven |
last post by:
I am using VC++ 6.0 to build a windows based application, however, my
application allows user to input command arguments in the dos prompt
windows. I want to have a warning message for user if they have input the
wrong arguments. How can I write text to the dos prompt windows if user
input the wrong arguments?
Thanks in advance.
Steven.
|
by: Kevin Walzer |
last post by:
I'm having difficulty structuring a Tkinter menu entry. Here is the
command in question:
self.finkmenu.add_command(label='Update List of Packages',
command=self.authorizeCommand(self.scanPackages))
When I start my program, it crashes because it's trying to run the
command self.authorizeCommand. The reason I'm structuring it in this
fashion is that this command takes another command as an argument--in
this case, self.ScanPackages.
|
by: Chris Carlen |
last post by:
Hi:
Having completed enough serial driver code for a TMS320F2812
microcontroller to talk to a terminal, I am now trying different
approaches to command interpretation.
I have a very simple command set consisting of several single letter
commands which take no arguments. A few additional single letter
commands take arguments:
|
by: jhatch2007a |
last post by:
I have just started learning visual basic 2005. I tried entering the command: Wait(1000) in order for the program to wait for 1 second before proceeding, but it rejected this saying Wait wasn't defined. How do I enter a pause between lines of code? Since I'm very new to visual basic, I would like to be able to follow things along on the screen without them whizzing right by.Thank you!
|
by: kyosohma |
last post by:
I have created what amounts to a simple GUI email sending program
using Python + wxPython. I have modified the mailto registration in
the Windows Registry so that it launches the script when someone
clicks on someone's email link in a web page.
While this works great if I create a convoluted path command as the
registry entry and pass in the email argument to the script, it
doesn't work at all if I turn my python file into an exe using...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |