0

I am attempting to execute this command from a Python script:

sed -i s/"exited_cleanly":false/"exited_cleanly":true/ ~/.config/chromium/Default/Preferences

When I run from the bash console, it succeeds.

But when I run with the following code, it does not:

    process = subprocess.Popen(['sed', '-i', 's/"exited_cleanly":false/"exited_cleanly":true/', '~/.config/chromium/Default/Preferences'], stdout=subprocess.PIPE)
    process.communicate()

    >>> sed: can't read ~/.config/chromium/Default/Preferences: No such file or directory

But this file clearly exists, I can find it, etc.

What's the issue? Am I missing something?

I'm on Python 3.9

Thanks!

Eduardo

Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
  • Consider Barmar's comments about it being much sounder to edit JSON _as JSON_ instead of as text echoed. – Charles Duffy Sep 15 '21 at 23:13
  • In a past life I had a customer who insisted on doing things that way (parsing our API responses with their own hand-rolled parser) and it made my life miserable; we'd change libraries or settings and our output would be modified in a way that any standard-compliant parser would ignore, but theirs would blow up and they'd be calling support. Treating JSON _as JSON_ instead of treating it as strings is less trouble for everyone, including your future self. – Charles Duffy Sep 15 '21 at 23:14

1 Answers1

2

~ is expanded by the shell, it's not part of the actual pathname. Since you're not using the shell to execute the command, it's not expanded. You can use the Python function os.path.expanduser() to do this.

import os

process = subprocess.Popen(['sed', '-i', 's/"exited_cleanly":false/"exited_cleanly":true/', os.path.expanduser('~/.config/chromium/Default/Preferences')], stdout=subprocess.PIPE)

I'm not sure why you're using sed to do this. Python can read and write the file itself. It also looks like you're trying to modify JSON using string operations. It would be better to use json.load() to read the file, modify the data, and then rewrite the file with json.dump().

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • HA! I found the answer at the same time haha I'll leave as I did a bit differently but accept your answer, of course. Thank you! – Edy Bourne Sep 15 '21 at 23:12