Question

so this piece of code is meant to take a line from a file and replace the certain line from the string with a new word/number, but it doesn't seem to work :(

else:
    with open('newfile', 'r+')as myfile:
           x=input("what would you like to change: \nname \ncolour \nnumber \nenter   option:")
           if x == "name":
               print("your current name is:")
               test_lines = myfile.readlines()
               print(test_lines[0])
               y=input("change name to:")
               content = (y)
               myfile.write(str.replace((test_lines[0]), str(content)))

I get the error message TypeError: replace() takes at least 2 arguments (1 given), i don't know why (content) is not accepted as an argument. This also happens for the code below

if x == "number":
          print ("your current fav. number is:")
          test_lines = myfile.readlines()
          print(test_lines[2])
          number=(int(input("times fav number by a number to get your new number \ne.g 5*2 = 10 \nnew number:")))
          result = (int(test_lines[2])*(number)) 
          print (result)
          myfile.write(str.replace((test_lines[2]), str(result)))





f=open('newfile', 'r')
print("now we will print the file:")
for line in f:
    print (line)
f.close
Was it helpful?

Solution

replace is a function of a 'str' object.

Sounds like you want to do something like (this is a guess not knowing your inputs)

test_lines[0].replace(test_lines[0],str(content))

I'm not sure what you're attempting to accomplish with the logic in there. looks like you want to remove that line completely and replace it?

also i'm unsure what you are trying to do with

content = (y)

the output of input is a str (which is what you want)

EDIT:

In your specific case (replacing a whole line) i would suggest just reassigning that item in the list. e.g.

test_lines[0] = content

To overwrite the file you will have to truncate it to avoid any race conditions. So once you have made your changes in memory, you should seek to the beginning, and rewrite everything.

# Your logic for replacing the line or desired changes
myfile.seek(0)
for l in test_lines:
    myfile.write("%s\n" % l)
myfile.truncate()

OTHER TIPS

Try this:

test_lines = myfile.readlines()
print(test_lines[0])
y = input("change name to:")
content = str(y)
myfile.write(test_lines[0].replace(test_lines[0], content))

You have no object known purely as str. The method replace() must be called on a string object. You can call it on test_lines[0] which refers to a string object.

However, you may need to change your actual program flow. However, this should circumvent the error.

You need to call it as test_lines[0].replace(test_lines[0],str(content))

Calling help(str.replace) at the interpreter.

replace(...) S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Couldn't find the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top