python input and output
python input and output
com/@Randommall/videos
0x0B-python-input_output/0-read_file.py
#!/usr/bin/python3
def read_file(filename=""):
"""Print the contents of a UTF8 text file to stdout."""
with open(filename, encoding="utf-8") as f:
print(f.read(), end="")
0x0B-python-input_output/1-write_file.py
#!/usr/bin/python3
0x0B-python-input_output/2-append_write.py
#!/usr/bin/python3
0x0B-python-input_output/3-to_json_string.py
#!/usr/bin/python3
def to_json_string(my_obj):
"""Return the JSON representation of a string object."""
return json.dumps(my_obj)
0x0B-python-input_output/4-from_json_string.py
#!/usr/bin/python3
def from_json_string(my_str):
"""Return the Python object representation of a JSON string."""
return json.loads(my_str)
0x0B-python-input_output/5-save_to_json_file.py
#!/usr/bin/python3
0x0B-python-input_output/6-load_from_json_file.py
#!/usr/bin/python3
def load_from_json_file(filename):
"""Create a Python object from a JSON file."""
with open(filename) as f:
return json.load(f)
0x0B-python-input_output/7-add_item.py
#!/usr/bin/python3
import sys
if __name__ == "__main__":
save_to_json_file = __import__('7-save_to_json_file').save_to_json_file
load_from_json_file = \
__import__('8-load_from_json_file').load_from_json_file
try:
items = load_from_json_file("add_item.json")
except FileNotFoundError:
items = []
items.extend(sys.argv[1:])
save_to_json_file(items, "add_item.json")
0x0B-python-input_output/8-class_to_json.py
#!/usr/bin/python3
0x0B-python-input_output/9-student.py
#!/usr/bin/python3
class Student:
"""Represent a student."""
def to_json(self):
"""Get a dictionary representation of the Student."""
return self.
0x0B-python-input_output/10-student.py
#!/usr/bin/python3
class Student:
"""Represent a student."""
0x0B-python-input_output/100-append_after.py
#!/usr/bin/python3
#!/usr/bin/python3
if __name__ == "__main__":
import sys
size = 0
status_codes = {}
valid_codes = ['200', '301', '400', '401', '403', '404', '405', '500']
count = 0
try:
for line in sys.stdin:
if count == 10:
print_stats(size, status_codes)
count = 1
else:
count += 1
line = line.split()
try:
size += int(line[-1])
except (IndexError, ValueError):
pass
try:
if line[-2] in valid_codes:
if status_codes.get(line[-2], -1) == -1:
status_codes[line[-2]] = 1
else:
status_codes[line[-2]] += 1
except IndexError:
pass
print_stats(size, status_codes)
except KeyboardInterrupt:
print_stats(size, status_codes)
raise
0x0B-python-input_output/11-student.py
#!/usr/bin/python3
class Student:
"""Represent a student."""