Wikipedia:Reference desk/Archives/Computing/2019 April 15

Computing desk
< April 14 << Mar | April | May >> Current desk >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


April 15

edit

How do you read a text file from elsewhere in a Python program?

edit

How do you read a text file from elsewhere in a Python program? I'm talking about a Python program being in one file and having a text file from elsewhere be imported into this file. Futurist110 (talk) 00:42, 15 April 2019 (UTC)[reply]

If you are just talking about reading a text file for data into a python program, you can use the open() function. See here. 74.88.70.115 (talk) 01:12, 15 April 2019 (UTC)[reply]
OK, but I meant to retrieve a file from elsewhere and put it into a program in a different file. Is open() really the only thing that one has to do? Futurist110 (talk) 03:55, 15 April 2019 (UTC)[reply]
Oh, you mean to insert the contents of the file as part of the program, like #include in a C program? I don't know how to do it in Python, so that's the limit of my contribution. --76.69.46.228 (talk) 04:24, 15 April 2019 (UTC)[reply]
So we need to distinguish a few things here. If you have a Python program foo.py and you wish to use bar.txt as data, such as simply printing the file, you can do something like
mytext = open("./bar.txt").read()
print (mytext)
Here, mytext is a string containing the entire contents of bar.txt
However, I think what you're asking is this: In program foo.py you want to be able to import the contents of another script bar.py, which is a Python file containing some functions, :::etc. In this case, you can simply import whatever you want. For example, if bar.py contains a function baz(), and both scripts are in the same directory, you can have in foo.py :::something like this:
import baz from bar
baz()
Just to cover all the bases, if you want to execute the entire contents of bar.txt in your script, then you would write something like:
myscript = open("./bar.txt").read()
exec(myscript)
There is another function eval() which has different properties, see here for all the details :::between the two. 69.122.136.2 (talk) 04:32, 15 April 2019 (UTC)[reply]
If you really want something like #include, please first convince yourself that you have a good reason to want this horrible thing. Yes you could use "exec" as 69.122.136.2 mentions. Normally you would use "import" which brings in the file as a module rather than as a raw chunk of program text. I think the file has to end in ".py" instead of ".txt" unless you use some inconvenient import hooks that it's better to avoid, so if you can use .py then that's preferable. What is the use case? You can probably get better advice if you say what problem you're actually trying to solve. E.g. if it's something like dynamically generated python code there might be a saner way to accomplish your goal. 67.164.113.165 (talk) 05:35, 18 April 2019 (UTC)[reply]
  • Assuming that you're auto-generating some Python code, or some big block of data, from another executable program, then the usual technique is to write that file out as a complete, compilable Python module of itself.
As a general rule, avoid #include mechanisms. As a really strong rule, avoid use of exec() or eval(). Andy Dingley (talk) 16:09, 18 April 2019 (UTC)[reply]