0

I am new to python and working on to get input and edit a file with python. The value I want to edit is "web-iphone" with the text I get from input.

Code so far:

web = raw_input("Enter value")

The file: test.py

    local {
            value web-iphone
    }
user3270211
  • 915
  • 4
  • 20
  • 42

1 Answers1

2

Edit: What you are asking is clearer now, refined my answer.

To get a file's contents:

def read_file(filename):
  return open(filename).read()

And to write to a file:

def write_file(filename, toWrite):
  file = open(filename, 'w')
  file.write(toWrite)
  file.close()

So to replace "web-iphone" with whatever the user typed in you could do:

Web = raw_input("Enter a value ")
Replaced = read_file("myfile.txt").replace("web-iphone", Web)
write_file("myfile.txt", Replaced)

For your comment:

newInput = raw_input("Enter a value ")
OldFile = read_file("myfile.txt")
value = OldFile.find("value"+6)
newFile = OldFile[:value] + newInput + OldFile[OldFile.find("\n",value+1):]
Leafthecat
  • 326
  • 1
  • 8