r/pythonhelp • • May 26 '22

Replace text in a file

Homework assignment. I have to open a file from a user input as read only. Close the file. Replace the text from the other user inputs. Open the file to write. Close it again. Then open and print.

file = input('Enter file name:')
old_word = input('Enter a string to be replaced: ')
new_word = input('Enter a string to be added: ')

with open(file, 'r') as infile:
    file = infile.read()
    
infile.close()

with open(file, 'w') as infile:
    file = infile.write()

file = file.replace(old_word, new_word)

I keep getting an "OSError: [Errno 22]" when it tries to open 'file' again. Any help would be appreciated.

2 Upvotes

3 comments sorted by

2

u/kaleidist May 26 '22
filename = input('Enter file name: ')
old_word = input('Enter a string to be replaced: ')
new_word = input('Enter a string to be added: ')

with open(filename, 'r') as infile:
    file = infile.read()

file = file.replace(old_word, new_word)

with open(filename, 'w') as ofile:
    ofile.write(file)

When you leave the with context, then the file is automatically closed. I renamed the variables so hopefully that makes it easier to see what you were doing wrong.

1

u/PuddleglumsLeftFoot Dec 06 '22

What would be a simple way to iterate this so that it could replace a large amount of words? So as to avoid doing

file = file.replace(old_word, new_word)
file = file.replace(old_word1, new_word1)
file = file.replace(old_word2, new_word2)
...

2

u/kaleidist Dec 06 '22

What you're describing is a bit unusual, because if you need to deal with sets of strings like that, then you should use a collection containing the strings, and not just multiple, separate strings.

That is, you wouldn't have old_word, old_world1, old_world2 etc. each containing one string. You would have old_words which would contain a list of those strings. Or, indeed, you would have a data structure like a dictionary which would inherently pair the old words with the new words.

And then you would use a for loop, here for two separate lists:

for i in count(len(old_words)):
    file = file.replace(old_words[i], new_words[i])

Here for a dictionary:

for old_word, new_word in words_dict.items():
    file = file.replace(old_word, new_word)

And here's a demonstration using/modifying the original poster's code from above:

filename = input('Enter file name: ')
old_words = input('Enter a set of strings to be replaced, each separated with a space: ').split()

with open(filename, 'rt') as infile:
    file = infile.read()

for word in old_words:
    file = file.replace(word, input(f'Enter a string to replace "{word}": '))
    
with open(filename, 'wt') as ofile:
    ofile.write(file)
print("Done!")

But to literally do what you're describing you would do something like:

for pair in [(old_word1, new_word1), (old_word2, new_word2), (old_word3, old_word3)]:
    file = file.replace(pair[0], pair[1])

But I want to reiterate, you wouldn't usually do this, because you would structure your data in such a way as to avoid naming all those variables.