Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Adding return to python files automagically

JREMOVEDt in case you were dying of curioREMOVEDity during the review session...





import os

def addReturn(dir):

  for file in os.listdir(dir):

    if file.endswith(".py"):
      python = open(dir + "//" + file, "r")
      lines = python.readlines()
      firstinfound = 0

      for line in lines:

        if (line.find("def ") > -1) and firstinfound == 0:
          firstinfound = 1
          start = line.find("(")
          end = line.find("):")
          input = line[start+1: end]

        elif firstinfound == 1:
          firstinfound = 2
          spaces = 0
          for char in line:
            if char == " ":
              spaces = spaces + 1
            else:
              break

        else:
          pass

      returnline = "\n" + spaces * " " + "return(" + input + ")"
      lines = lines + [returnline]
      python.close()

      python = open(dir + "//" + file, "wt")

      for line in lines:
        python.write(line)

      python.close()

And no, it won't be on the test.
-Student1680


Questions? Comments?



Link to this Page