Perl to Python

Tim Hammerquist tim at
Tue Nov 27 04:57:49 CET 2001

Paul Malinowski <paul.malinowski at> graced us by uttering:
> I am relatively new to programming and very new to Python.
> I have a number of Perl/CGI scripts on a Linux/Apache web
> server that run queries to 
> MS Access databases through an Openlink service running on
> an NT server.  Works great!  I have been trying to do the 
> same thing with Python and am not having much luck.  Would
> it be to much to ask one of you Python guru's to help me
> translate this from Perl to Python?  (I intentionally left
> out the paths in the Perl environment variables to the 
> OpenLink installed client on my Linux system to hide user 
> names and such...)
> Any help would be really appreciated!
> Paul Malinowski

w/o writing the program for you:

> #Setting some environment variables.
> $ENV{UDBCINI}="~openlinksw/bin/udbc.ini";
> $ENV{ODBCINI}="~openlinksw/bin/odbc.ini";
> $ENV{ODBCINSTINI}="~openlinksw/bin/odbcinst.ini";
> $ENV{LD_LIBRARY_PATH}="$ENV{LD_LIBRARY_PATH}:~openlinksw/lib";
> $ENV{SHLIB_PATH}="$ENV{SHLIB_PATH}:~openlinksw/lib";
> $ENV{PATH}="$ENV{PATH}:~openlinksw/bin:~openlinksw/samples/ODBC";

Environment vars can be set using the os.environ dictionary. eg:

    $ENV{DISPLAY} = ":0.0";         # perl
    os.environ['DISPLAY'] = ":0.0"  # python

> #SQL query that runs qryModelSNforWeb in the warranty Access database.
> $sql = "echo \"select *  from <Query in MS Access Database>;\" |
> odbctest DSN=<OpenLink Definition to MS Access DataBase>";
> if (!open(SQL,"$sql |")) {
>     print "Unable to open connect to database<br>";
>     exit 0;
> }
> if (open(SQL, "$sql |")) {
>     chomp(@rawdata = <SQL>);
> }

Pipes can be opened using os.popen().  Pipes can be read/written by
reading from/writing to the file object returned by os.popen(). Read
from this as normal in Python, just as you're reading from the SQL
filehandle in the perl code.

    # Perl
    open PIPE, "ls /etc" or die "can't read from pipe: $!\n";
    print while <PIPE>;
    close PIPE;

    # Python (roughly)
    pipe = os.popen('ls /etc')
    while 1:
        line = pipe.readline()
        if not line:
        print line
    retval = pipe.close()

BTW: many people here don't know Perl and many don't want to talk
about Perl in a Python newsgroup.

Tim Hammerquist
Is that a 286 or are you just running Windows?

More information about the Python-list mailing list