Edited
When I answered I focused strictly on the question itself, but then I saw in the comments that the author’s intention is to do something similar to devops
or infra management using python, and then I saw that the best answer is @jsbueno and, to complement, I decided to edit my answer with an example (prototype) from 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 that 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 q at the end of the script there is a command to print the same.
~/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 the answer "original":
To illustrate 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 function foo
within hello.py
$ python -c 'import hello; hello.foo()'
bar
And now the hello function:
$ python -c 'import hello; hello.hello()'
hello world!
by linux terminal, you mean running a bash?
– zwitterion
that’s right, the idea is to create a
arquivo.py
for creating standard directories and files!– Leandro Paixão