So I made a file editing program in python… and one of the functions isn’t working right

  python, python-2.7

As the title says, I made a file editing program with python.

Here is the code that I’m have a problem with:

def fileError(file):
  raise OSError("file {} does not exist".format(file))

class AccessFile():
  def fileExists(self, file):
    import os
    return bool(os.path.exists(file))

  def filecreate(self, file):
    if not self.fileExists(file):
      with open(file, "w") as f:
    else: raise OSError("file {} already exists".format(file))

  def filedelete(self, file):
    import os
    if self.fileExists(file):
    else: fileError(file)

  def fileread(self, file):
    #check if file exists
    if self.fileExists(file):
      #detect length of file
      with open(file, "r") as f:
        line = " "
        x = 0
        while line != "":
          line = f.readline()
          x += 1
      #piece lines together in a list
      filelines = []
      with open(file, "r") as f:
        for i in range(x - 1):
      #return a tuple
      return tuple(filelines)
    else: fileError(file)

  def filewrite(self, file, line, text):
    ''' BUG: apparently this either overwrites the line its writing or appends
    to the line its writing... make up your mind!'''
    if self.fileExists(file):
      #get file contents
      filelines = list(self.fileread(file))
      #see if line parameter is out of range or not
        filelines[line] = text
      except IndexError:
        for i in range(line - len(filelines)):
        filelines.append(str(text) + "n")
      #apply changes
      with open(file, "w") as f:
        f.write("") #delete contents
      with open(file, "w") as f:
        for l in filelines:
    else: fileError(file)

  def fileoverwrite(self, file, data):
    #if there is no file to delete, it will make a new one
    x = 0
    for line in data:
      self.filewrite(file, x, line)
      x += 1

accessfile = AccessFile()

The bug is in the filewrite(self, file, line, text) function. When called, it either writes a new line (which is what I want it to do), appends to the line its supposed to replace, or just doesn’t write any lines at all.

Say I want to write a python file with this program:

from fileEditing import *

file = "/Users/ashton/Desktop/Atom/Python/FileEditing/"
data = [
"from fileEditing import *",
"class FileEditing():",
"  def __init__(options, immutable_files):"
"    self.options, self.immutable_files = options, immutable_files",
"    ",
"  def prompt():",
"    "
"while True:",
"  pass"

accessfile.fileoverwrite(file, data)

When I run it, it makes a file with accessfile.fileoverwrite(file, data), like its supposed to.

But thats where things get whacky.

( below)

from fileEditing import *

class FileEditing():
  def __init__(options, immutable_files):    self.options, self.immutable_files = options, immutable_files
  def prompt():
    while True:

If you know how to fix the filewrite(self, file, line, text), please let me know.

(I use python 2.7 but python 3 is fine)

Source: Python Questions