I have created a memo.py file in the 'C: / Python' directory using the Pycharm program.
# C:/Python/memo.py
import sys
option = sys.argv[1]
memo = sys.argv[2]
print(option)
print(memo)
Where do I enter the content?
C:\Python>python memo.py -a "Life is too short"
cmd or Python interpreter or pycharm?
When you want to pass command line arguments you can run it with cmd, but you should be in the folder where file exists. Also you have to make sure python is installed it there.
You can call it like this
python memo.py "Life is too short" "one more argument"
Because you are program expect here for two arguments. or else you can try like :
python memo.py Life is
where this will consider the word after space like second argument
The o/p for above will be like :
Life is too short
one more argument
second :
Life
is
You can run the code using command line as Vikas suggested. If you really want to run the code using Pycharm, go to Run-->Edit configurations in Pycharm. Then in the "Script parameters" option write your arguments in double quotes separated by space and write -s in the "Interpreter" option. Then save the configuration and simply run the code using Run-->Run option
Related
First time ever asking for help here so my apologies if i'm missing something relevant.
I am learning Python for data science (very new), but figured I could use it to automate some of my other work.
I'll post what I have so far, but first I'll explain what I'm trying to do:
Normally (without Python) I open up the command prompt, change directories, and then paste a string (directly into command prompt) that looks like:
"program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD"
here is what I have so far:
import os
mail_date = input(("What is the mailing date?(YYYY-MM-DD): "))
os.system("start cmd.exe #cmd /k CD C:\Path")
# Next string would look like:
# "program.exe C:\file path to config file\config.cfg -extra_parameters1 -extra_parameters2 -extra_parameters3 -Mail_Date=YYYY-MM-DD
So far I can run the Python script, input a mailing date as a variable, open command prompt, then change directories. I am not sure how to then pass the "program.exe..." string to the command line.
I have this command line text that I want to run inside my python script to the command line. Any suggestions on how to do this?
explorer "ms-drive-to:?
destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"
If you want to effectively run this in python shell,like this: # it will open windows map
>>> !explorer "ms-drive-to:?
destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"
If you want to run in code, do like others method.All the answers are correct.
import os
command_str = 'explorer "ms-drive-to:? destination.latitude=47.680504&destination.longitude=-122.328262&destination.name=Green Lake"'
os.system(command_str)
# it will open windows map, and driving directions to Green Lake from your current location.
Be careful to use double quotes, single quotes will not be recognized correctly!
windows uwp info: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/launch-maps-app
Sure, you can do something like is described in this post:
https://raspberrypi.stackexchange.com/questions/17017/how-do-i-run-a-command-line-command-in-a-python-script
Seems like you should use subprocess:
Running Bash commands in Python
Suppose I have a python file main.py, and it has some optional parameters, --learning-rate, --batch-size, and etc.
If I want to run this file, I can input the following into the terminal(Ubuntu Linux for example).
python3 main.py --learning-rate 0.1 --batch-size 100
And now, I want to write some code in main.py, in order that after I enter the command above, I can get this command in a string by executing those code. The following is the string I want to get:
"python3 main.py --learning-rate 0.1 --batch-size 100"
The reason I want to do this is that I want to write this string into my recording file so that I can know better what command I have run.
Could anyone tell me what package should I import and what code should I write to get that command information during running the python file?
Thanks!
You cannot always get precisely what you typed, because the shell will have first done substitutions and expanded filenames before starting your script. For example, if you type python "foo.py" *.txt, your script won't see *.txt, it will see the list of files, and it won't see the quotes around foo.py.
With that caveat out of the way, the sys module has a variable named argv that contains all of the arguments. argv[0] is the name of the script.
To get the name of the python executable you can use sys.executable.
To tie it all together, you can do something like this:
print(sys.executable + " " + " ".join(sys.argv))
Why not just remake the command using the arguments you parsed? It won't be exactly what you typed, but that might be nice as it will be in a common format.
Ex (assuming the learning rate and batch size are stored in similarly named variables):
command = "python3 main.py --learning-rate {} --batch-size {}".format(learning_rate, batch_size)
Of course it will be a little more complicated if some commands are optional, but I assume in that case there would be a default value for these parameters, since your network would need those parameters every time.
I am attempting to run this .PY file from Command Prompt:
# Merge two .BSG files
# Starting block and world position are taken from the first file
# Example: "bsgmerge input.bsg output.bsg merged.bsg"
import io, sys
one = open(sys.argv[1]).readlines()
two = open(sys.argv[2]).readlines()
for n in [1,3,5,7,9,11,17,19,21,23]:
one[n] = one[n][:-1]+"|"+two[n].partition("|")[2]
open(sys.argv[3],"w").write("".join(one))
It is a program that takes a creation from the game Beseige and merges it with another saved creation so that opening the merged file results in both creations being present. If you want more details, you can read up on that here.
I am having trouble figuring out how to call this program from the command line. At first I thought the problem was me having Python 2 (it requires Python 3), so I uninstalled 2 and installed 3. This did not help.
What I am doing is entering the "python" command to pull up the Python environment within CMD, then entering the command to call the program based on the third comment in the file ("bsgmerge input.bsg output.bsg merged.bsg").
I tried using full file paths or simply changing to the correct directory before typing the "python" command and using only the file names, but so far I've had no luck.
When I am in the correct directory, then enter the Python environment, typing the command "bsgmerge 1.bsg 2.bsg M.bsg" (my existing files to be merged are 1.bsg and 2.bsg), this error occurs:
File "<stdin>", line 1
bsgmerge 1.bsg 2.bsg M.bsg
^
SyntaxError: invalid syntax
I took a Python course (which is why I used to have Python 2 on my machine) last fall, so I noticed that there is no "def" defining a function in the above code, which is something I've never encountered, so I'm thinking that is the root of my problems.
Thanks in advance for the help.
I was probably same problem with python launcher.
If you use Linux, first line shoud be:
#! /path/to/your/python/3
In Windows it some more complicated:
In registry by regedit change
HKEY_CLASSES_ROOT\Python.File\shell\open\command from "C:\Python27\python.exe" "%1" %* to "C:\Windows\py.exe" "%1" %*.
And first line of script shoud be:
#! python3
Now it shoud work properly.
I would like to run a python script from enthought canopy v1.5.0.2717, either in mac or windows, and provide a absolute file path as an argument using the run configuration dialog.
In the run configuration I put an argument (for example):
'/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'
The my script contains the following code:
import sys
print sys.argv[1]
I then click "run" and the printed string is:
'/Users/dir/Data/University'
Another example is using the path:
'C:\User\Program files\test.txt'
and it prints
'C:UserProgram'
It looks like it splits the path at the spaces, and deletes the "\".
Running the script from the command line like:
$python myScript.py '/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'
Results in the correct printed string:
'/Users/dir/Data/University stuff/CQT/Data/ScriptRunFolder/testingPythonRfRamp.xml'
How can I achieve the same result using Canopy?
The shell will group everything within double-quotes into a single parameter, not single quotes.
>type showme.py
import sys
print(sys.argv[1:])
>python showme.py 'i am sad'
["'i", 'am', "sad'"]
>python showme.py "i am happy"
['i am happy']