-1

I want to import some external files in python. How can I do it?

Will it work with import module or there is some other way to do it?

e.g my file name is Hex.txt.

Meet Adhia
  • 29
  • 1
  • 4
  • 2
    you have to open the file and read from it, here is a link to get started : https://docs.python.org/2/tutorial/inputoutput.html – Stack Jul 28 '17 at 14:09

2 Answers2

0

You can grab the file and loop the lines to do stuff with like this:

filehandle = open("Hex.txt", "r")
for line in filehandle:
    print (line)

Or just grab the whole file into a string:

with open("Hex.txt", "r") as myfile:
    data=myfile.read()

There are actually several different ways to do this depending on what you are planning on doing with the data. Search around on StackOverflow but this should get you started.

sniperd
  • 5,124
  • 6
  • 28
  • 44
0

You don`t need to import text files. Import statement is for importing python built-in and external libraries and for importing .py script if you need to do so.

For example:

import sys
    print sys.path

In case you need to do something with text file u can read it with following instructions:

with open(file_path) as f:
    print f.read() #prints files content

Read more about writing and reading files in Pythons documentation.

If you meant something else in your question please provide more detailed information about what you ant to do.

Michael
  • 1,170
  • 2
  • 14
  • 35