how to input file in python code example
Example 1: how to write user input to a file in python
x = input("Enter blah : ")
y = open('C:\Users\USERNAME\Documents\Doc1.txt', 'w')
y.write(x)
x = input("Enter blah : ")
y = open('C:\Users\USERNAME\Documents\Doc1.txt', 'wb')
y.write(x)
Example 2: python write to file
with open(filename,"w") as f:
f.write('Hello World')
Example 3: python write to file
path = "guide/README.txt"
with open(path, "w") as fil:
fil.write("This is the README. It is reccomended that you read it.")
fil.close()
'''
List of methods:
w* - replace everything with needed text
r^ - read the file
a* - adds to file
x - creates file
* Creates file if the file at that path does not exist
^ Throws error if file does not exist
'''
Example 4: file input python
import fileinput
for line in fileinput.input(files ='gfg.txt'):
print(line)