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. |
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)
open()
function. See
here.
74.88.70.115 (
talk)
01:12, 15 April 2019 (UTC)
mytext = open("./bar.txt").read()
print (mytext)
mytext
is a string containing the entire contents of bar.txtbaz()
, and both scripts are in the same directory, you can have in foo.py :::something like this:import baz from bar
baz()
myscript = open("./bar.txt").read()
exec(myscript)
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)#include
mechanisms. As a really strong rule, avoid use of exec()
or eval()
.
Andy Dingley (
talk)
16:09, 18 April 2019 (UTC)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. |
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)
open()
function. See
here.
74.88.70.115 (
talk)
01:12, 15 April 2019 (UTC)
mytext = open("./bar.txt").read()
print (mytext)
mytext
is a string containing the entire contents of bar.txtbaz()
, and both scripts are in the same directory, you can have in foo.py :::something like this:import baz from bar
baz()
myscript = open("./bar.txt").read()
exec(myscript)
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)#include
mechanisms. As a really strong rule, avoid use of exec()
or eval()
.
Andy Dingley (
talk)
16:09, 18 April 2019 (UTC)