Re: having trouble importing a module from local directory

2006-05-12 Thread Marcelo Ramos
vduber6er escribió:
> First off this is in unix.
>
> I have a C file that has python embedded in it.  In the script I have
> the following
>
> PyRun_SimpleString(code);
>
> where
> code = "import mymodule"
>
> however I get this error
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> ImportError: No module named mymodule
> Object not found
> Segmentation fault
>
> If i just run python from my local path then type "import mymodule" in
> the python prompt this is fine.  I'm guessing when I am embedding my
> python script in the C code the local directory is no longer the
> directory where my C code resides.  How can i tell python to load the
> module from my current local directory?  
>
>   

Try adding the directory where your mymodule module lives to the 
PYTHONPATH environment variable before running your c program.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass variables between scripts?

2006-05-12 Thread Marcelo Ramos
Gross, Dorit (SDRN) escribió:
> Dear list,
>
> I am an absolute newbie to python and would appreciate your help very much
> :)
>
> I am trying to write a little python script that wraps a set of external
> scripts. The external scripts are either also written in python or are simple
> bash scripts. My wrapping script should be able to send an argument to the
> first script, to execute it, to read its results and to send them then as
> arguments into the next script.
>
> This is the first part that I have written so far: 
>
> #! /usr/local/bin/python
> # test_exec.py
>
> import os, sys, glob
>
> fileList = glob.glob('/data/*.ZIP')
>
> for f in fileList:
>   try: 
>   globvars = {'infile' : f}
>   locvars = {}
>   execfile('/scripts/second.py', globvars(), locvars)
>   except IOError:
>   exit(0)
>   print locvars
>
>
> And this is what happens when calling test_exec.py
>
>  ./test_exec.py 
> Traceback (most recent call last):
>   File "./test_exec.py", line 19, in ?
> execfile('/scripts/second.py', vars(), results)
> TypeError: 'dict' object is not callable
>
>
> I tried already to modify the script in different ways but wasn't successful
> so far to make it running. Could you maybe help what I am missing? Further, I
> am not sure how the second python has to look like to actually read what is
> given in "globvars" and to sent its results into "locvars". 
>
> Or might os.popen* be a better option?  
>
>
>   

You are calling the dictionary globvars as a function then the error. 
The fixed line is:

execfile('/scripts/second.py', globvars, locvars)



What you want is the function globals().
Try putting this line in second.py:

print globals()['infile']

Using the dictionary returned by globals() you can make second.py to 
read the contents of testexec.py's globvars dictionary.
locvars is populated with the local variables of second.py and that is 
what you want.


Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unzip zip files

2006-05-12 Thread Marcelo Ramos
DataSmash escribió:
> I need to unzip all zip file(s) in the current directory
> into their own subdirectories.  The zip file name(s) always
> start with the string "usa" and end with ".zip".
> The code below will make the subdirectory, move the zip
> file into the subdirectory, but unzips the contents into the
> root (current) directory.  I want the contents of the zip file
> unloaded into the newly created subdirectory where the zip file is.
>
> Any ideas?
> Thanks.
> R.D.
>
> import subprocess
>
> # Get all the zip files in the current directory.
> for zip in os.listdir(''):
> if zip.endswith(".zip"):
>
> # Remove the first 3 and the last 4 characters
> # e.g. usa12345.zip becomes 12345
> zipBase = zip[3:-4]
>
> # Make directory for unzipping
> os.mkdir(zipBase)
>
> # Move the zip file to the subdirectory
> shutil.move(zip, zipBase)
>
> # Make system call "unzip"
>     subprocess.Popen(["unzip", zipBase + "\\" + zip]).wait()
>
>   

See "-d" zip's parameter in man zip.

Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending mail with attachment...

2006-05-15 Thread Marcelo Ramos
Merrigan escribió:
> Hi,
>
> I have now eventually finished my newbie-backup script and I'm very
> proud of the way it functions...
>
> Anyways, I am looking for an easy way to use smtplib to send an email
> with the output log of the script to multiple accounts. I need to use
> it with a smtp server, and cannot pipe it directly to sendmail.
>
> I have tried about 50 different ways that I have googled for in the
> last 6 hours, but none of them work, I keep on getting errors.
>
> The script runs on a Linux system.
>
> Thanks for any help.
>
>   

I have a python script in production doing what you want using smtplib 
and it works perfectly. Send us your code and the errors you are getting.


Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to pass variables between scripts?

2006-05-16 Thread Marcelo Ramos
Gross, Dorit (SDRN) escribió:
>>> #! /usr/local/bin/python
>>> # test_exec.py
>>>
>>> import os, sys, glob
>>>
>>> fileList = glob.glob('/data/*.ZIP')
>>>
>>> for f in fileList:
>>> try: 
>>> globvars = {'infile' : f}
>>> locvars = {}
>>> execfile('/scripts/second.py', globvars(), locvars)
>>> except IOError:
>>> exit(0)
>>> print locvars
>>>
>>>
>>>   
>> You are calling the dictionary globvars as a function then the error. 
>> The fixed line is:
>>
>> execfile('/scripts/second.py', globvars, locvars)
>>
>>
>>
>> What you want is the function globals().
>> Try putting this line in second.py:
>>
>> print globals()['infile']
>>
>> Using the dictionary returned by globals() you can make second.py to 
>> read the contents of testexec.py's globvars dictionary.
>> locvars is populated with the local variables of second.py 
>> and that is 
>> what you want.
>>
>> 
>
> Marcelo, thank you! Passing the variables with dictonaries and function
> globals() works fine if no other functions are defined in 'second.py'. Now
> 'second.py' contains further functions and a "if __name__ = __main__"
> statement and in this case it seems that 'second.py' is not fully executed
> from 'test_exec.py'. For the sole purpose of testing, 'second.py' looks like
> this at the moment:
>
> #! /usr/local/bin/python
> # second.py
>
> import os, sys
>
> global zipfile
> print 'Read from globals: ' + globals()['infile']
> zipfile = globals()['infile']
> print 'Read from zipfile: ' + zipfile
>
> if __name__ == '__main__':
>
>   print 'Hello'
>   print globals()['infile']
>   print zipfile
>
>   
> Calling test_exec.py results into this output:
>
>  ./test_exec.py 
> Read from globals: /data/S0012230_0010.ZIP
> Read from zipfile: /data/S0012230_0010.ZIP
>
>
> It seems that the commands within the main are not executed when calling
> test_exec.py!?! Is there a way to make it running?
>
> Regards and thank you again, 
> Dorit
>
>
>   
If you print __name__ in second.py its value is '__builtin__', because 
of that your "main" doesn't execute.
Try adding this to test_exec.py before the execfile() call:

globvars[ '__name__' ] = '__main__'

It looks like a ad hoc fix but i couldn't find any doc about the change 
of __name__ to 'builtin' of a python script being run
from another with execfile().

Regards.

-- 
Marcelo Ramos
Fedora Core 5 | 2.6.16
Socio UYLUG Nro 125

-- 
http://mail.python.org/mailman/listinfo/python-list