Input by input file

In this section, we are going to learn about how we can take input from an input file. Taking  input from an input file is easier in Python. We are going to look at an example for this. But first, we are going to create a simple text file called sample.txt and we'll write the following code in it:

Sample.txt:

Hello World
Hello Python

Now, create a script called accept_by_input_file.py and write the following code in it:

i = open('sample.txt','r')
o = open('sample_output.txt','w')

a = i.read()
o.write(a)

Run the program and you will get the following output:

$ python3 accept_by_input_file.py
$ cat sample_output.txt
Hello World
Hello Python
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset