0

I just want to have some ideas to know how to do that...

I have a python script that parses log files, the log name I give it as an argument so that when i want to run the script it's like that.. ( python myscript.py LOGNAME ) what I'd like to do is to have two scripts one that contains the functions and another that has only the main function so i don't know how to be able to give the argument when i run it from the second script.


here's my second script's code:

import sys
import os 

path = "/myscript.py"
sys.path.append(os.path.abspath(path))
import myscript 

mainFunction()

the error i have is:

script, name = argv 
valueError: need more than 1 value to unpack
Kirk ERW
  • 167
  • 1
  • 2
  • 14
  • `path = "./myscript.py"`? Also, this error most likely originates from `myscript.py` and not this script considering you don't even use `sys.argv` in this script – Torxed May 06 '13 at 07:28
  • The error indicates you have 3 arguments in argv but are only unpacking two. Maybe show us more code? (From `myscript`) – Benjamin Gruenbaum May 06 '13 at 07:29
  • 1
    The answer is: Both scrips share the same `sys.argv` – Torxed May 06 '13 at 07:30

2 Answers2

1

Python (just as most languages) will share parameters across imports and includes. Meaning that if you do:

python mysecondscript.py heeey that will flow down into myscript.py as well. So, check your arguments that you pass.

Script one

myscript = __import__('myscript')
myscript.mainfunction()

script two

import sys
def mainfunction():
    print sys.argv

And do:

python script_one.py parameter

You should get:

["script_one.py", "parameter"]

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • As mentioned in a comment on snakes solution. If you need to modify the parameter then do so by supplying `mainfunction()` with a parameter. – Torxed May 06 '13 at 07:45
0

You have several ways of doing it.

>>> execfile('filename.py')

Check the following link:

How to execute a file within the python interpreter?

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82