How do I print the content of a .txt file in Python?

Question:

I’m very new to programming (obviously) and really advanced computer stuff in general. I’ve only have basic computer knowledge, so I decided I wanted to learn more. Thus I’m teaching myself (through videos and ebooks) how to program.

Anyways, I’m working on a piece of code that will open a file, print out the contents on the screen, ask you if you want to edit/delete/etc the contents, do it, and then re-print out the results and ask you for confirmation to save.

I’m stuck at the printing the contents of the file. I don’t know what command to use to do this. I’ve tried typing in several commands previously but here is the latest I’ve tried and no the code isn’t complete:

from sys import argv

script, filename = argv
print "Who are you?"
name = raw_input()

print "What file are you looking for today?"
file = raw_input()

print (file)

print "Ok then, here's the file you wanted." 

print "Would you like to delete the contents? Yes or No?"

I’m trying to write these practice codes to include as much as I’ve learned thus far. Also I’m working on Ubuntu 13.04 and Python 2.7.4 if that makes any difference. Thanks for any help thus far 🙂

Asked By: Courtney

||

Answers:

Just do this:

>>> with open("path/to/file") as f: # The with keyword automatically closes the file when you are done
...     print f.read()

This will print the file in the terminal.

Answered By: user2555451
with open("filename.txt", "w+") as file:
  for line in file:
    print line

This with statement automatically opens and closes it for you and you can iterate over the lines of the file with a simple for loop

Answered By: Stephan

This will give you the contents of a file separated, line-by-line in a list:

with open('xyz.txt') as f_obj:
    f_obj.readlines()
Answered By: Noel Evans

Opening a file in python for reading is easy:

f = open('example.txt', 'r')

To get everything in the file, just use read()

file_contents = f.read()

And to print the contents, just do:

print (file_contents)

Don’t forget to close the file when you’re done.

f.close()
Answered By: Greg

to input a file:

fin = open(filename) #filename should be a string type: e.g filename = 'file.txt'

to output this file you can do:

for element in fin:
    print element 

if the elements are a string you’d better add this before print:

element = element.strip()

strip() remove notations like this: /n

Answered By: SamuraiT

print ''.join(file('example.txt'))

Answered By: Ninja420

How to read and print the content of a txt file

Assume you got a file called file.txt that you want to read in a program and the content is this:

this is the content of the file
with open you can read it and
then with a loop you can print it
on the screen. Using enconding='utf-8'
you avoid some strange convertions of
caracters. With strip(), you avoid printing
an empty line between each (not empty) line

You can read this content: write the following script in notepad:

with open("file.txt", "r", encoding="utf-8") as file:
    for line in file:
        print(line.strip())

save it as readfile.py for example, in the same folder of the txt file.

Then you run it (shift + right click of the mouse and select the prompt from the contextual menu) writing in the prompt:

C:examples> python readfile.py

You should get this. Play attention to the word, they have to be written just as you see them and to the indentation. It is important in python. Use always the same indentation in each file (4 spaces are good).

output

this is the content of the file
with open you can read it and
then with a loop you can print it
on the screen. Using enconding='utf-8'
you avoid some strange convertions of
caracters. With strip(), you avoid printing
an empty line between each (not empty) line
Answered By: PythonProgrammi

It’s pretty simple

#Opening file
f= open('sample.txt')
#reading everything in file
r=f.read()
#reading at particular index
r=f.read(1)
#print
print(r)

Presenting snapshot from my visual studio IDE.

enter image description here

Answered By: Manjeet

Reading and printing the content of a text file (.txt) in Python3

Consider this as the content of text file with the name world.txt:

Hello World! This is an example of Content of the Text file we are about to read and print
using python!

First we will open this file by doing this:

file= open("world.txt", 'r')

Now we will get the content of file in a variable using .read() like this:

content_of_file= file.read()

Finally we will just print the content_of_file variable using print command.

print(content_of_file)

Output:

Hello World! This is an example of Content of the Text file we are about to read and print
using python!

Answered By: user11936470

single line to read/print contents of a file

reading file : example.txt

 print(open('example.txt', 'r').read()) 

output:

u r reading the contents of example.txt file

Answered By: ANDuser
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.