user input and files python code example

Example 1: how to write user input to a file in python

#Print User Input to A File
x = input("Enter blah : ")
y = open('C:\Users\USERNAME\Documents\Doc1.txt', 'w')
y.write(x)

#Convert text to binary and then write to file
x = input("Enter blah : ")
y = open('C:\Users\USERNAME\Documents\Doc1.txt', 'wb')
y.write(x)

Example 2: file input python

# import fileinput 
import fileinput 
  
# Using fileinput.input() method 
for line in fileinput.input(files ='gfg.txt'): 
    print(line)