z/OS Tools & Language

Expand all | Collapse all

Python sample code files

  • 1.  Python sample code files

    Posted 07-13-2016 21:21

    I have just installed your Python 2.7.6 under Z/os 2.1. So far so good.

    Do you have a couple of sample programs which show how to use the open() to read the three types of files available: 1) Z/os datasets, 2) USS files, 3) DDNAMEs?

    I am guessing that 3) can’t be done, but certainly 1) and 2) would be great!

    Thanks



  • 2.  RE: Python sample code files

    Posted 07-14-2016 15:14

    I would disagree: option 3 is doable if the implementer chooses to support it.

    For example: Dovetailed Software provides additional shell oriented functionality (think along the lines of ‘cat’ and not python stuff) that references DDDEFs with the syntax //DD:SAMPLEDD while DSNs are in //SAMPLE.DSN.NAME(MEMBER).

    Sorry that I can’t give you any python examples, though!



  • 3.  RE: Python sample code files

    Posted 12-05-2016 10:30

    oe2a.py.doc (2.8 KB)

    The attached file has the translation tables between the open edition code page 1047 and the latin1 (819) (The .doc was added to the file name so that it could be uploaded)

    from oe2a import tt1047_819
    f2=open("//'MM.JCL(TEST)'")
    for line in f2.read().translate(tt1047_819).split('\n'):
        print line
    

    The above Python code does the following:

    1. opens PDS member TEST in dataset MM.JCL for reading
    2. reads the complete file
    3. translates the text to ascii
    4. splits the text by lines and
    5. prints the lines

    Note the special file name syntax for z/OS datasets. The syntax is described in XL C/C++ Programming Guide / Chapter 10 / Opening Files.

    :slight_smile:



  • 4.  RE: Python sample code files

    Posted 09-09-2019 08:38

    Dear mister,
    have you got an example how to write to MVS data set information (for example, My_Arr which is a numpy array) to force it be written in Latin1 encoding?



  • 5.  RE: Python sample code files

    Posted 12-06-2016 12:55

    ad Point 3) reading from files defined by DD names.

    Here is an example for running a python script from a batch job.

    //MMPY     JOB MM,CLASS=K,MSGCLASS=X                      
    //*                                                       
    //* Batch Unix Program Spawn Local (BPXBATSL)             
    //*                                                       
    //* Note: - Environment Variables are not expanded in PARM  
    //*       - STDENV needed to set env (profile not used)     
    //*       - STDPARM overcomes line limitation, parameters   
    //*         may be split over several lines                 
    //*                                                         
    //BPX      EXEC PGM=BPXBATSL,                             
    //    PARM='PGM /u/mm/py27/bin/python /u/mm/apy/ddreader.py'   
    //*                                                       
    //DDIN     DD DISP=SHR,DSN=MM.JCL(PY2PUB)                 
    //*                                                       
    //STDOUT   DD SYSOUT=*                                    
    //STDERR   DD SYSOUT=*                                    
    //STDENV   DD *                                           
    PY27=/u/mm/py27                                           
    LIBPATH=/u/mm/py27/lib:/lib:/usr/lib:.                    
    PYTHONHOME=/u/mm/py27                                     
    PYTHONPATH=/u/mm/py27/lib/python2.7                       
    _BPXK_AUTOCVT="ON"                                        
    _BPX_SHAREAS=MUST                                         
    _BPX_BATCH_SPAWN=YES                                      
    //                                                        
    

    Important is to use the BPXBATSL that does the spawn of the python program instead of BPXBATCH which does an exec that creates a new job step for python thus loosing the DD names.

    See also Unix System Services Command Reference
    Appendix D. Running shell scripts or executable files under
    MVS environments

    The DDIN DD name is being used in the python script ddreader.py

    from oe2a import tt1047_819                              
    f2=open('//DD:DDIN','r')                                 
    print '--- Reading from //DD:DDIN'                         
    for line in f2.read().translate(tt1047_819).split('\n'): 
        print line                                           
    print '--- ddreader.py terminated'
    

    Obviously, the file tagging does not work for z/OS files, so the python program needs to handle text conversion itself. Due to the swapped nl,lf byte codes the standard EBCDIC encodings e.g. cp037 provided with Python do not work for line breaks.

    Would be nice if the Rocket Python would provide these encodings so the kludgy code with the translate table could be replaced by a readline from a file that was opened with encoding.

    :slight_smile:



  • 6.  RE: Python sample code files

    Posted 12-09-2016 15:36

    Hi M_Mueller,
    Are you using the 2.7.6 port of python or the new 2.7.12 which is in beta?



  • 7.  RE: Python sample code files

    Posted 12-12-2016 10:32

    I have used the 2.7.12 Beta2. Should there be a difference?
    Probably, beginning with 2.7.12 internal text is in ASCII and not in EBCDIC and there are conversion complexities.

    So in Python 2.7.6 reading can be simplified:

    f=open("//'MM.JCL(TEST)'")
    for line in f:
        print line,
    

    The I/O routines split the lines after the ‘\n’ which is \x15.
    (With the comma after the “print line” there will be no additional line feed).