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

前端 未结 9 969
慢半拍i
慢半拍i 2020-12-13 18:28

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

相关标签:
9条回答
  • 2020-12-13 19:02

    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()
    
    0 讨论(0)
  • 2020-12-13 19:02

    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!

    0 讨论(0)
  • 2020-12-13 19:04

    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.

    0 讨论(0)
  • 2020-12-13 19:05

    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.

    0 讨论(0)
  • 2020-12-13 19:06

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

    0 讨论(0)
  • 2020-12-13 19:10

    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
    
    0 讨论(0)
提交回复
热议问题