importing external .txt file in python

importing external .txt file in python

You can import modules but not text files. If you want to print the content do the following:

Open a text file for reading:

f = open(words.txt, r)

Store content in a variable:

content = f.read()

Print content of this file:

print(content)

After youre done close a file:

f.close()

As you cant import a .txt file, I would suggest to read words this way.

list_ = open(world.txt).read().split()

importing external .txt file in python

The import keyword is for attaching python definitions that are created external to the current python program. So in your case, where you just want to read a file with some text in it, use:

text = open(words.txt, rb).read()

Leave a Reply

Your email address will not be published. Required fields are marked *