1

I have VSAM file in the unix system. I want to read the file using the layout of that file in the python. Out of the .idx and .dta, I copied .dta to my local machine and tried to read using the below code,

infile = open("myfile.dta","r",encoding="ansi")
for line in infile:
    print(line)

without the encoding parameter it is giving the error..

"UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 1572"

So to solve that error i opened the file in the notepad++ and checked the encoding. Now I can read the file and it displays the data (still I can see few special characters).

Now the main question is how can I read this file record by record as per the provided layout.

Naresh
  • 16,698
  • 6
  • 112
  • 113
Nilesh
  • 69
  • 1
  • 11
  • Is this Mainframe VSAM file ???; is it a Cobol Copybook ??? – Bruce Martin Feb 28 '19 at 10:06
  • If it is from the mainframe, ideally copy the VSAM file to a Text file on the mainframe (or at the very least a VB file) and transfer this new file – Bruce Martin Feb 28 '19 at 10:43
  • @BruceMartin This is Mainframe VSAM file. I cant save and read it because there are many such files which I want to read and those are getting updated daily. It will be very difficult for me to get latest data if i use it by saving it. Please suggest. – Nilesh Feb 28 '19 at 11:22

2 Answers2

4

There are ports of Python 3 and Python 2 to z/OS. It looks like the Python 3 port does not currently have support for accessing "native" or "classic" z/OS files -- those that do not reside in the z/Unix file system.

VSAM is not a small topic. If you're interested in the history and underlying technologies, feel free to search for "what is VSAM" in your favorite search engine; the TLDR is that VSAM files are analogous to ISAM in that they allow reading of a particular record given a key. VSAM has other capabilities of course, and it is emphatically not ISAM, that's just an analogy.

Depending on the usage pattern for the files in question you may run into some resistance to your access. If these VSAM files are in use by a production CICS region, heavy use from your code may create contention resulting in performance degradation.

Something to consider: you are essentially adding a new requirement to a running production system, doing so requires some analysis to determine the best mechanism to satisfy your requirement without having a negative impact on that existing system. That mechanism will take into account existing shop standards, security, performance, staff time, etc. Maybe that analysis has already taken place (I cannot know if it has) but your question indicates you have a copy of a single VSAM file on your workstation and subsequent comments seem to indicate you wish to access "many such files" in place on z/OS.

As is often the case when non-mainframe developers must access some or all of the data contained in an existing mainframe system, you must discuss your requirements and theirs to come up with a mutually agreeable solution. I have tried to outline some of the issues in this answer, this answer, and this answer to this question which has references to Calcite (with which I have no experience) and NFS Server capabilities of z/OS (with which I also have no experience). Lots of capabilities, lots of options, and I will reiterate here something from more than one of the linked answers:

Please understand there is a big difference between...

  • what is technically possible
  • what is allowed in your shop
  • what is likely to provide a robust and maintainable solution given your requirements

These are three very different things. Some of us have life experiences that make us reticent about answering questions regarding what is technically possible absent any mention of what is allowed in your shop or what the actual business requirement is that is being solved.

Mainframes have been around for over half a century, and many shops have standard solutions to technical problems. Sometimes the solution is "don't do that, and here's what we do instead." Working against the recommendations of your technical staff, or your shop standards, is career limiting.

Update 2022-10-29

David Crayford has just published a Python library for z/OS dataset I/O, including VSAM, at https://github.com/daveyc/pyzfile.

cschneid
  • 10,237
  • 1
  • 28
  • 39
  • Thank you for this information. I am Mainframe developer and I can understand your point regarding the accessing the VSAM file which is already open in the CICS region. Actually I am working on the Microfocus COBOL and OS is Sun Solaris. The files I am going to read are not prod files so it wont be a problem for me. Is there any way I can access these files as I am not on Z/OS – Nilesh Mar 01 '19 at 04:24
  • @Nilesh I'm sorry but I have no experience with the Micro Focus ecosystem. – cschneid Mar 01 '19 at 04:53
1

You won't be able to read a VSAM file using Python. Perhaps if you call into the C API libraries, but that is doubtful. You can use the Java JZOS api and reach into the MVS side of things. Most z/OS systems have Java installed. If you don't have Java installed... go learn some COBOL.

Kenny
  • 316
  • 1
  • 9