Uhm, this is my first question that I do in Stack Overflow, so let's just go to the point. :)
I'm creating a program with Python v3.6 and it is a like terminal, my plan is do like the programming languages(C#: "console.write"). The commands will be stored in a folder called "lib" and it will have folders to separate the commands, so the thing will be just like this:
------------------------------------------------------------------------------------------------------------------------------------
lib-
console-
write.py
see.py
main.py
------------------------------------------------------------------------------------------------------------------------------------
So, as you can see there's the folder "lib", inside it has the folder "console" and inside the folder has 2 files "write.py" and "see.py". My mission is when the terminal starts the user type a command like: "$ console.write Hello World", doing that the program will separate the "console" from "write" creating a list ["console", "write"]. Now the terminal will see if "console" is a folder, if it exists so the terminal will check if "write"+".py" is a file, if so it will import the file and pass the args "Hello World", the basic structure that the command needs is two lines: "class exec:" and "def main(args):":
class exec:
def main(args):
# The rest of the command here.
print(args)
With everything allright the terminal will import the file and execute the class "exec" and execute the def "main" passing all the arguments to it resulting in:
$ console.write Hello World
Hello World
$
The same to "see.py":
$ console.see /home/user/documents/file.txt
This is a file that contains some text, really interesting.
$
Thx if someone help me. ;) Have a good day!
Related
I know this question has been asked in various variations but none of them seem to work for my specific case:
(btw all mentioned files are in my PATH)
I have a simple python script called test.py:
import sys
print('Hello world!')
print(sys.argv)
counter = 0
while True:
counter +=1
The counter is just there to keep the command window open so don't worry about that.
When I enter
test.py test test
into cmd I get the following output:
Hello world!
['C:\\Users\\path\\to\\test.py']
For some reason unknown to me the two other commands (sys.argv[1] and sys.argv[2]) are missing.
However when I create a .bat file like this:
#C:\Users\path\to\python.exe C:\Users\path\to\test.py %*
and call it in cmd
test.bat test test
I get my desired output:
Hello world!
['C:\\Users\\path\\to\\test.py', 'test', 'test']
I've read that the
%*
in the .bat file means that all command line arguments are passed to the python script but why are exactly these arguments not passed to the python script when I explicitly call it in cmd?
From what I've read all command line arguments entered after the script name should be passed to said script but for some reason it doesn't work that way.
What am I overlooking here?
I'm guessing that you need to actually run the script through the command line with C:\Users\path\to\python.exe test.py test test.
I'm not sure how Windows handles just test.py test test but from my limited experience it is probably just trying to open all 3 of those files. Since the first one (test.py) has a .py extension, it is opened with the Python Interpreter and is run automatically. The other two are not actually being passed in as an argument.
I have a python code in which at the beginning it takes a string variable let say "element_name" from user and build some sub-folders based on this string and also some output files created by this code move to those folders.
On the other hand, I have a bash script in which some codes should be running in the sub-folders made in python code.
Any help how to introduce those folders in bash? How to pass the "element_name" from python to bash?
In python code "a.py" I tried
first = subprocess.Popen(['/bin/echo', element_name], stdout=subprocess.PIPE)
second = subprocess.Popen(['bash', 'path/to/script', '--args'], stdin=first.stdout)
and then in bash
source a.py
echo $element_name
but it doesn't work.
It's not clear from your question what is in your scripts, but I guess
subprocess.run(['/bin/bash', 'path/to/script', '--args', element_name])
is doing what you intend to do, passing the value of element_name to script as an argument.
I found a way. What I did is to pass the argument in a bash file and import this bash file as a source to my main bash file. Now everything works well.
I have registered with exercism.io on the Python track, and haven't got off to a good start! The first exercise is a simple print hello world example, and I am of course able to write the code that executes this. The problem I have is where on earth do I place my code? Should I overwrite the existing hello_world.py file with my own file, or add my script lines to the existing file? I have read the documentation and must be missing something as I can't fathom out what to do with my code to test and submit.
When I download the test material, there is a default hello.world.py file created in the relevant directory, which contains this;
def hello():
pass
There is also a hello_world_test.py that contains this;
import unittest
import hello_world
# Tests adapted from `problem-specifications//canonical-data.json` # v1.1.0
class HelloWorldTest(unittest.TestCase):
def test_hello(self):
self.assertEqual(hello_world.hello(), 'Hello, World!')
if __name__ == '__main__':
unittest.main()
I have written a file called exercism_hello_world.py which contains this;
# This script prints "Hello, World!" to the console
print ("Hello, World!")
# end of script
Can anyone who may already be using exercism.io please advise how / where I place my code so that I can test / submit the first exercise and continue with the learning. Thanks.
After installing the cli script.
Enter the file location of full python file along with the name of the file.
Example
exercism submit C:\Users\srag\Exercism\python\hello-world\hello_world.py
You want the Exercism directory to begin with a capital "E" if you're running on macOS.
Try out:
exercism submit /Users/(your username)/Exercism/python/hello-world/hello_world.py
You should add your solution to the hello_world.py file.
However, you can change the default Exercism workspace directory. If you are on MacOS or Unix you can do that via terminal:
exercism configure --workspace="YOUR_PATH"
By changing this setting, everytime you run the command to "clone" the problem, it will get copied to the path you specified.
Regarding this problem, I am not sure if it helps, but it was stated to return "Hello, World!" and not print it.
I've been given a piece of code which is a physical model (filename 'agnsim.py') and some instructions to run it which I'm confused by.
The instructions say that I should import the code using
import agnsim as agn
and then to run the model with
ed = agn.Wilp(dens=3., incr=0.2, drac=2.0)
The argument above in Wilp will configure the run.
My question is, how do I actually run this? Do I create a separate .py file that contains these two lines of code?
I've only ever run simple python programs before using e.g. >>>python file.py
You should literally just open up the python console and type those two lines in.
$python
import agnsim as agn
ed = agn.Wilp(dens=3., incr=0.2, drac=2.0)
Make sure that agnsim.py is located at the same folder level from which you start python. E.g. If you're in "My Documents" and "agnsim.py" is in my documents, you should cd to "My Documents" and then start python there (from the command line).
When I import a selfmade module and run the program, the output is what I expected. However when I update the module and run the program in the same console, the previous result is shown. If I open a new console, then the new result is correctly shown.
Let's take an example:
# Filename: myfunctions.py
def helloWorld():
print("Hello World")
# Filename: runfuction.py
from myfunctions import helloWorld
helloWorld()
The output is Hello World. When I replace in myfunctions.py Hello into Bye, and when I run the program in the same console, my result is still Hello World, and not Bye World. The updated text Bye World will be only shown when I open a new console.
try:
reload(module_name)
that's how it works in a local python console. I don't have a PythonAnywhere account, but I would guess it's pretty similar.
Note that any object instances you have already created will not be changed, but this (or something similar) should work fine for functions.