Edited
When I replied I focused strictly on the question itself, but then I saw in the comments that the author's intention is to do something like devops
or infra-management using python, and then I saw that the best answer is @jsbueno and, for complement, I have decided to edit my answer with an example (prototype) of argpasse
that he quotes
createdir.py
#!/usr/bin/python
import argparse
import sys
parser = argparse.ArgumentParser(
description='Script para criacao de diretorios e arquivos padroes!')
parser.add_argument('-v', '--verbose',action="store_true", help="verbose output" )
parser.add_argument('-R', action="store_false",
help="Copia recursiva de todos os arquivos e diretorios")
parser.add_argument('dirname', type=str, help="Diretorio a ser criado")
parser.add_argument('filename', type=argparse.FileType('w'),
help="Arquivo a ser criado")
args = parser.parse_args()
print (args)
## Implemente o resto daqui em diante
Now let's make the script executable:
$ chomd +x createdir.py
After this test on the command line, because of the first line #!/usr/bin/python
it is not necessary to call pyton, you can call the script directly:
$./createdir -h
Exit:
Script para criacao de diretorios e arquivos padroes!
positional arguments:
dirname Diretorio a ser criado
filename Arquivo a ser criado
optional arguments:
-h, --help show this help message and exit
-v, --verbose verbose output
-R Copia recursiva de todos os arquivos e diretorios
Now let's call with arguments, see at the end of the script there is a command to give a print to them.
~/createdir.py -R /home/admin arquivo1
Namespace(R=False, dirname='/home/admin', filename=<open file 'arquivo1',
mode 'w' at 0x7f632dc47300>, verbose=False)
From this point on the "original" answer:
For example, let's create a file called hello.py
with the following content:
def hello():
print ('hello world!')
def foo():
print ('bar')
Now let's call the foo
function inside hello.py
$ python -c 'import hello; hello.foo()'
bar
And now the hello function:
$ python -c 'import hello; hello.hello()'
hello world!