Saturday, 7 September 2013

Python add character to string

Python add character to string

I am trying to open a user's text file and replace a string in Python. I
have the replacement working, but to open a file I understand that I need
to add another backslash after each one in the file path. I am not sure
how to do that. I looked at other stack overflow questions, but they were
mostly about adding to the beginning or end of the string. Please help!
Here's the code so far:
yourfile = input()
with open ("C:\\Users\\Rajrishi\\Documents\\MyJava\\text.txt") as myfile:
data = myfile.readlines()
strdata = "".join(data)
strdata = strdata.replace("a string","a replacement")
print(strdata)

No comments:

Post a Comment