How to Save Input to a File in Python


This example simply shows us how to save input into a file with Python.

2.Codes

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

filename = input('Input file name:\n')
fp = open(filename, "w")
ch = input('Input a string:\n')
while ch != '%':
    fp.write(ch + '\n')
    sys.stdout.write(ch + '\n')
    ch = input('')
fp.close()

When running the codes, you need the first input file name and the string to save. After that, you can see the content exists when you open the file.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments