4 ms·
Okay, I did it. import subprocess import re import yaml output = subprocess.check_output(['ls', '-l'], universal_newlines=True) matching_l
by radus 7y ago
Okay, I did it.
import subprocess
import re
import yaml
output = subprocess.check_output(['ls', '-l'], universal_newlines=True)
matching_lines = []
for line in output.splitlines():
if re.search(r'total', line):
matching_lines.append(line)
with open('matching_lines.yaml', 'w') as f:
f.write(yaml.dump(matching_lines))
Python 3.7: https://repl.it/repls/PotableDamagedAutocad https://repl.it/repls/PotableDamagedAutocad
Python 2.7: https://repl.it/repls/OrchidDirtyNotification https://repl.it/repls/OrchidDirtyNotification
- mikepurvis 7y agoNice. Yeah my life got a lot easier when I learned about `universal_newlines` changing the subprocess return type under Python 3. The much worse case of this that I dealt with a few years ago was reading in and modifying XML files, and then saving the modified XML contents as strings in a yaml file (a keyed cache). The input XML files (which I didn't control) were not consistent about having the encoding marked, and that really made things a mess for ElementTree. A taste: https://github.com/ros-infrastructure/rosdistro/search?q=utf-8&unscoped_q=utf-8 https://github.com/ros-infrastructure/rosdistro/search?q=utf...
- deleted 7y ago[deleted]
- slavik81 7y agoThis program would crash with a UnicodeEncodeError upon encountering any file with a non-latin character in its name on Windows with Python 3.5 or earlier. I know that's not the version of Python you were targeting, and I know you didn't write this program to be cross-platform, but I wrote a similar program and I was repeatedly surprised by those sorts of problems. I don't entirely agree with mikepurvis, but I do feel his pain.