0

i'm trying to change a text file using python. removing every line that contains a certain string

i stumble on an error using the os.replace method.

Traceback (most recent call last):
  File "test.py", line 12, in <module>
    os.replace('/home/johnny/Documents/Python/temp.txt', '/home/johnny/Documents/Python/Soothing.txt')
AttributeError: 'module' object has no attribute 'replace'

here's my code:

import os

with open("/home/johnny/Documents/Python/Soothing.txt", "r") as input:
    with open("/home/johnny/Documents/Python/temp.txt", "w") as output:
        # iterate all lines from file
        for line in input:
            # if line starts with given substring then don't write it in temp file
            if not line.strip("\n").startswith('stuff i dont like'):
                output.write(line)

# replace file with original name
os.replace('/home/johnny/Documents/Python/temp.txt', '/home/johnny/Documents/Python/Soothing.txt')

the code is in a file.py format i execute in a linux shell.

trying to figure out what goes wrong, i tried:

-importing os in the linux shell to no avail, typing import os

-changing the directory to a simple file name ==> no good either

the output of the thingy is a temp file with the proper change performed but i'm unable to rewrite the original file

any help's welcome.

PS

-OS: linux (ubuntu 18.04)

-python 2.7.17 aliasing it to 3.6.9 as explained here it gets worse and gives:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    for line in input:
  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe8 in position 253: invalid continuation byte

then i tried python3 test.py==> same error

8
  • 3
    is it possible that you have os.py somewhere? Commented Sep 5, 2021 at 17:50
  • What happens if you try print(os.__file__)? Commented Sep 5, 2021 at 17:56
  • @JohnnyBravo Your code would work in Python 3, but I get your error when I run it in Python 2.7. What command are you using to run your code? If you're using python my_code.py, than try python3 my_code.py. Maybe you could specify the Python version number you're using in your question. Commented Sep 5, 2021 at 18:20
  • Could you attach your pip package versions by creating a new virtual environment, installing all your pip packages and then writing them to a file with pip freeze > requirements.txt ? Commented Sep 6, 2021 at 6:57
  • 1
    Python 3 on your system expects text files to be encoded in UTF-8 by default. It looks like your file uses a different encoding. You have to specify it when opening the file with open("/home/johnny/Documents/Python/Soothing.txt", encoding=<your actual encoding>). You have a list of the standard encodings at docs.python.org/3/library/codecs.html#standard-encodings Commented Sep 6, 2021 at 19:36

3 Answers 3

1

You actually trying to rename the file.
You should use os.rename() method, so your code will be:

import os

with open("/home/johnny/Documents/Python/Soothing.txt", "r") as input:
    with open("/home/johnny/Documents/Python/temp.txt", "w") as output:
        # iterate all lines from file
        for line in input:
            # if line starts with given substring then don't write it in temp file
            if not line.strip("\n").startswith('stuff i don't like'):
                output.write(line)

# replace file with original name
os.rename('/home/johnny/Documents/Python/temp.txt', '/home/johnny/Documents/Python/Soothing.txt')
Sign up to request clarification or add additional context in comments.

4 Comments

Do bear in mind that when a string contains an apostrohe in it ', that string needs to be double-quoted". So the line if not line.strip("\n").startswith('stuff i don't like'): would be if not line.strip("\n").startswith("stuff i don't like"):. Just saying.
replace and rename are similar (bear in mind that replace is more cross platform than rename).
@SamMatzko you are right, I answered regarding the replace method only, all the rest just copy-pasted.
@Constantin thanks for your comment. I used to use replace for replacing the string content only...
1

The function call is correct, although you may have something conflicting with the module os in your file. I suggest importing os.replace this way:

from os import replace

If it is still conflicting you could import the replace function to as you want, like:

from os import replace as rename_file

4 Comments

If the OP gets AttributeError: 'module' object has no attribute 'replace', there is no way from os import replace would allow him to access a replace attribute in the os module.
@ThierryLathuille makes sense
@Constantin This answer would work in Python 3 (I tested it in 3.8) so you are right there. But the the error the OP mentioned gets raised when I run it in Python 2.7. I looked in 2.7's os file, and there is no attribute replace. The question should have specified what version of Python was being used to run it.
@Constantin: you asked me to install a new virtual environment. how's it done? do you mean some sort of virtual machine?
1

The answer (thanks to @Constantin and @Thierry Lathuille):

  • Execute in python3: for some reason import os doesn't work in 2.x
  • Change the targeted file encoding (e.g. UTF-8)

6 Comments

Will it let you accept your own answer? If you could, that would be nice, so that others who have the same problem can see which the answer solved the problem.
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@SamMatzko it could have been cool. apparently the elders of stackoverflow are of a different mind... dunno why...
@INDRAJITH Could you please explain why this "does not provide an answer to the question"? For example, does it not explain in enought detail? Have these solutions been tested and found not to work? It would be nice if you could be more specific.
@INDRAJITH Ah. I looked in the edit history for this answer, and I see what you mean.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.