Edit text in file in python

user3270211

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
    }
Leafthecat

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):]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related