SPSS won't open dataset in Python
<p>I'm trying to manipulate SPSS datasets using a python script. The problem is, my code won't open the active dataset. The problematic part is this:</p>

<pre><code>spss.StartDataStep()                     
dataset = spss.Dataset()
print len(dataset.cases)
</code></pre>

<p>This code will create a datasetobject if I manually open a .sav file and run the code. But in my program, it simply creates an empty dataset, even though I clearly have an active dataset running. How do I fix this?</p>

<p>Here's the error:</p>

<p><code>Traceback (most recent call last): 
  File "&lt;string&gt;", line 128, in &lt;module&gt; 
ValueError: __len__() should return &gt;= 0</code></p>

<pre><code>BEGIN PROGRAM.
import SpssClient,spssaux, spss, os
SpssClient.StartClient()
path='c:\users\myDocs\'

def inlezen(path,filename):
            if filename.startswith("Dec_gemeente"): 
                           vars=' /1 Code1 0-3 A4   Label1 4-33 A30'
            if filename.startswith("Dec_post"): 
                           vars=' /1 Code1 0-3 A4   Label1 4-27 A24   Code2 28-32 A5  Label2 33-67 A35'
            if filename.startswith("Dec_etniciteit_kort"): 
                           vars=' /1 Code1 0-0 A1  Label1 1-23 A23'
            if filename.startswith("Dec_etniciteit_lang"): 
                           vars=' /1 Code1 0-1 A2  Label1 2-33 A32  Code2 34-34 A1  Label2 35-58 A24  Code3 59-59 A1  Label3 60-95 A36'
            if filename.startswith("Dec_geslacht"): 
                           vars= ' /1 Code1 0-0 A1  Label1 1-8 A8'
            if filename.startswith("Dec_landcode"): 
                           vars=' /1 Code1 0-3 A4  Label1 4-43 A40  Code2 44-44 A1  Label2 45-56 A12  Code3 57-58 A2  Label3 59-89 A31'
            if filename.startswith("Dec_nationaliteit"): 
                           vars=' /1 Code1 0-3 A4  Label1 4-43 A40  Code2 44-44 A1  Label2 45-56 A12  Code3 57-58 A2  Label3 59-89 A31'
            if filename.startswith("Dec_verbl"): 
                           vars=' /1 Code1 0-1 A2  Label1 2-84 A83'

            bestand=path+filename

            spss.Submit("""
            DATASET CLOSE ALL.
            GET DATA
            /TYPE=TXT
            /FILE='%s'
            /FIXCASE=1
            /ARRANGEMENT=FIXED
            /FIRSTCASE=1
            /IMPORTCASE=ALL
            /VARIABLES=%s.
            DATASET NAME LabelSet WINDOW=FRONT.
            DATASET ACTIVATE LabelSet.
            EXECUTE."""
            %(bestand,vars))



def wegschrijven(filename):

            labels1=''
            labels2=''
            labels3=''

            # Labels maken 1.
            varnaam=filename

            labels1='VAR LAB' + ' ' + varnaam + " '" + varnaam + "'" + '.' + '\n' + 'VAL LAB'+' '+varnaam
            for i in range(len(dataset.cases)):
               for j in range(2):
                  if j==0:
                     temp=str(dataset.cases[i, j])[2:-2]
                     labels1 = labels1 + '\n' + temp + ' '
                  if j==1:
                     temp=str(dataset.cases[i, j])[2:-2]
                     labels1 = labels1 + "'" + temp + "'"
            labels1=labels1+'.'


            try:
               # Labels maken 2.
               varnaam='test'
               labels2='VAR LAB' + ' ' + varnaam + " '" + varnaam + "'" + '.' + '\n' + 'VAL LAB'+' '+varnaam

               for i in range(len(dataset.cases)):
                  for j in range(2,4):
                     if j==2:
                        temp=str(dataset.cases[i, j])[2:-2]
                        labels2 = labels2 + '\n' + temp + ' '
                     if j==3:
                        temp=str(dataset.cases[i, j])[2:-2]
                        labels2 = labels2 + "'" + temp + "'"
               labels2=labels2+'.'
            except:
               labels2=''
               print 'geen var 2'

            try:
               # Labels maken 3.
               varnaam='test'
               labels3='VAR LAB' + ' ' + varnaam + " '" + varnaam + "'" + '.' + '\n' + 'VAL LAB'+' '+varnaam

               for i in range(len(dataset.cases)):
                  for j in range(4,6):
                     if j==4:
                        temp=str(dataset.cases[i, j])[2:-2]
                        labels3 = labels3 + '\n' + temp + ' '
                     if j==5:
                        temp=str(dataset.cases[i, j])[2:-2]
                        labels3 = labels3 + "'" + temp + "'"
               labels3=labels3+'.'
            except:
               labels3=''
               print 'geen var 3' 



            output= labels1 + '\n' + '\n' + labels2 + '\n' + '\n' + labels3
            print output

            # Output naar syntaxdoc schrijven

            NewSyntaxDoc = SpssClient.NewSyntaxDoc()
            NewSyntaxDoc.SetAsDesignatedSyntaxDoc()

            # Inhoud syntax wijzigen.
            NewSyntaxDoc.SetSyntax(output)

            # Syntaxdocument opslaan.
            pad='I:/geg/WON/1 cijfer/LABELS/spsfiles/Labelbestanden/'+filename+'.sps'
            NewSyntaxDoc.SaveAs(pad)




for root, dirs, files in os.walk(path):
      for filename in files:
          inlezen(path,filename)

          # Toegang krijgen tot data editor.
          spss.StartDataStep()                     
          dataset = spss.Dataset()
          print len(dataset.cases)


          # Wegschrijven labels naar .sps.
          wegschrijven(filename)  
          dataset.close()  
          spss.EndDataStep()             
END PROGRAM.
</code></pre>