How do i display an entire file in python?

I'm trying to get Python to print the contents of a file:

log = open("/path/to/my/file.txt", "r")
print str(log)

Gives me the output:


Instead of printing the file. The file just has one short string of text in it, and when I do the opposite (writing the user_input from my Python script to that same file) it works properly.

edit: I see what Python thinks I'm asking it, I'm just wondering what the command to print something from inside a file is.

asked Aug 17, 2013 at 17:57

3

It is better to handle this with "with" to close the descriptor automatically for you. This will work with both 2.7 and python 3.

with open('/path/to/my/file.txt', 'r') as f:
    print(f.read())

answered Aug 17, 2013 at 18:27

How do i display an entire file in python?

László PappLászló Papp

50.4k39 gold badges108 silver badges134 bronze badges

1

open gives you an iterator that doesn't automatically load the whole file at once. It iterates by line so you can write a loop like so:

for line in log:
    print(line)

If all you want to do is print the contents of the file to screen, you can use print(log.read())

answered Aug 17, 2013 at 18:00

open() will actually open a file object for you to read. If your intention is to read the complete contents of the file into the log variable then you should use read()

log = open("/path/to/my/file.txt", "r").read()
print log

That will print out the contents of the file.

answered Aug 17, 2013 at 18:33

OliOli

5514 silver badges10 bronze badges

file_o=open("/path/to/my/file.txt")   //creates an object file_o to access the file
content=file_o.read()                 //file is read using the created object
print(content)                        //print-out the contents of file
file_o.close()

How do i display an entire file in python?

answered Aug 17, 2013 at 19:26

rogue_leaderrogue_leader

6061 gold badge5 silver badges17 bronze badges

  • Problem Formulation
  • Standard File Reading and Printing
  • How to Read all Lines of a File into a List (One-Liner)?
  • How to Read a File Line-By-Line and Store Into a List?

Problem Formulation

Given the path to a text file such as /path/to/file.txt.

How to read all the content from the file and print it to the Python standard output?

Standard File Reading and Printing

The standard approach to read the contents from a file and print them to the standard output works in four steps:

  1. Open the file.
  2. Read the content.
  3. Print the content.
  4. Close the file.

Let’s dive into each of those four steps next.

Here’s how this whole process looks like on my computer:

How do i display an entire file in python?

Step 1: Open the file for reading using the built-in open() function with the text file path as the first string argument and the reading mode 'r' as the second argument. Assign the resulting file object to a variable (e.g., named f).

f = open('/path/to/file.txt', 'r')

Step 2: Read the whole textual content from the file using the file.read() method and store it in a variable (e.g., named content). If your file consists of multiple lines, the resulting string will contain newline characters '\n' for each line break.

content = f.read()

Step 3: Print the file content by passing the content variable into the built-in print() function.

print(content)

Step 4: Close the file to clean up your code. This is a good practice according to the Python standard.

f.close()

Taken together, the proper code to read the contents of a text file and print it to the standard output looks like this:

f = open('/path/to/file.txt', 'r')
content = f.read()
print(content)
f.close()

Please note that you need to replace the string '/path/to/file.txt' with your own path to the file you want to read.


Do you need some more background? No problem, watch my in-depth tutorial about Python’s open() function:

Python open() Function – An 80/20 Guide by Example

How to Read all Lines of a File into a List (One-Liner)?

You can also read all lines of a file into a list using only a single line of code:

print([line.strip() for line in open("file.txt")])

To learn how this works, visit my in-depth blog article or watch the following video tutorial:

Python One-Liners - Trick 2 Read File and Strip() Lines

How to Read a File Line-By-Line and Store Into a List?

A more conservative, and more readable approach of achieving this is given in the following code snippet:

with open('file.txt') as f:
    content = f.readlines()

# Remove whitespace characters like '\n' at the end of each line
lines = [x.strip() for x in content]
print(lines)

You can see this in action in this blog tutorial and the following video guide:

How to Read a File Line By Line and Store Into a List

Hey, you’ve read over the whole article—I hope you learned something today! To make sure your learning habit stays intact, why not download some Python cheat sheets and join our free email academy with lots of free Python tutorials?

How do i display an entire file in python?

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

How do you display a file in Python?

Python File Open.
❮ Previous Next ❯.
f = open("demofile.txt", "r") print(f.read()) ... .
Open a file on a different location: ... .
Return the 5 first characters of the file: ... .
Read one line of the file: ... .
Read two lines of the file: ... .
Loop through the file line by line: ... .
Close the file when you are finish with it:.

How do I read an entire file in Python?

text = f. read() (Can try these in >>> Interpreter, running Python3 in a folder that has a text file in it we can read, such as the "wordcount" folder.) Read the whole file into 1 string - less code and bother than going line by line. ... .
lines = f. readlines() f. readlines() returns a list of strings, 1 for each line..

Which method is used to display entire contents of the file in Python?

Explanation: read function is used to read all the lines in a file.

How do you print the contents of a text file in Python?

Use file..
a_file = open("sample.txt").
lines = a_file. readlines().
for line in lines:.
print(line).
a_file. close().