How do I read a text file as a string?
up vote
-3
down vote
favorite
Below are the codes I have tried to read the text in the text file in a method called check_keyword()
def check_keyword():
with open(unknown.txt, "r") as text_file:
unknown = text_file.readlines()
return unknown
This is how i called the method:
dataanalysis.category_analysis.check_keyword()
The text in the text file:
Hello this is a new text file
There is no output for the method above :((
python text-files
add a comment |
up vote
-3
down vote
favorite
Below are the codes I have tried to read the text in the text file in a method called check_keyword()
def check_keyword():
with open(unknown.txt, "r") as text_file:
unknown = text_file.readlines()
return unknown
This is how i called the method:
dataanalysis.category_analysis.check_keyword()
The text in the text file:
Hello this is a new text file
There is no output for the method above :((
python text-files
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Below are the codes I have tried to read the text in the text file in a method called check_keyword()
def check_keyword():
with open(unknown.txt, "r") as text_file:
unknown = text_file.readlines()
return unknown
This is how i called the method:
dataanalysis.category_analysis.check_keyword()
The text in the text file:
Hello this is a new text file
There is no output for the method above :((
python text-files
Below are the codes I have tried to read the text in the text file in a method called check_keyword()
def check_keyword():
with open(unknown.txt, "r") as text_file:
unknown = text_file.readlines()
return unknown
This is how i called the method:
dataanalysis.category_analysis.check_keyword()
The text in the text file:
Hello this is a new text file
There is no output for the method above :((
python text-files
python text-files
edited 11 hours ago
asked Nov 8 at 9:24
School
619
619
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
1
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
text_file.readlines()
returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read()
instead.
You also have another problem in your code, you are trying to open unknown.txt
, but you should be trying to open 'unknown.txt'
(a string with the file name).
New contributor
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
text_file.readlines()
returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read()
instead.
You also have another problem in your code, you are trying to open unknown.txt
, but you should be trying to open 'unknown.txt'
(a string with the file name).
New contributor
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
2
down vote
accepted
text_file.readlines()
returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read()
instead.
You also have another problem in your code, you are trying to open unknown.txt
, but you should be trying to open 'unknown.txt'
(a string with the file name).
New contributor
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
text_file.readlines()
returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read()
instead.
You also have another problem in your code, you are trying to open unknown.txt
, but you should be trying to open 'unknown.txt'
(a string with the file name).
New contributor
text_file.readlines()
returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read()
instead.
You also have another problem in your code, you are trying to open unknown.txt
, but you should be trying to open 'unknown.txt'
(a string with the file name).
New contributor
New contributor
answered Nov 8 at 9:30
Tomothy32
3366
3366
New contributor
New contributor
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printing
unknown
? This should work, so please post your updated code.– Tomothy32
Nov 8 at 9:44
@School Can you try printing
unknown
? This should work, so please post your updated code.– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
answered Nov 8 at 9:29
bak2trak
46739
46739
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
Problem solved, thanks!
– School
Nov 8 at 10:58
Problem solved, thanks!
– School
Nov 8 at 10:58
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
You can do this as follows:
with open("foo","r") as f:
string = f.read()
edited Nov 8 at 9:29
Aran-Fey
20.3k53266
20.3k53266
answered Nov 8 at 9:29
power.puffed
115
115
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204752%2fhow-do-i-read-a-text-file-as-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32