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 :((










share|improve this question




















  • 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















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 :((










share|improve this question




















  • 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













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 :((










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:27

























asked Nov 8 at 9:24









School

599




599








  • 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




    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












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).






share|improve this answer





















  • 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 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


















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.






share|improve this answer





















  • Problem solved, thanks!
    – School
    Nov 8 at 10:58


















up vote
1
down vote













You can do this as follows:



with open("foo","r") as f:
string = f.read()





share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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















Required, but never shown

























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).






share|improve this answer





















  • 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 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















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).






share|improve this answer





















  • 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 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













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).






share|improve this answer












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).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 9:30









Tomothy32

1,13413




1,13413












  • 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 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


















  • 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 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
















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












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.






share|improve this answer





















  • Problem solved, thanks!
    – School
    Nov 8 at 10:58















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.






share|improve this answer





















  • Problem solved, thanks!
    – School
    Nov 8 at 10:58













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.






share|improve this answer












Instead of text_file.readlines() use text_file.read() which will give you contents of files in string format rather than list.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 9:29









bak2trak

46739




46739












  • 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




Problem solved, thanks!
– School
Nov 8 at 10:58










up vote
1
down vote













You can do this as follows:



with open("foo","r") as f:
string = f.read()





share|improve this answer























  • 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















up vote
1
down vote













You can do this as follows:



with open("foo","r") as f:
string = f.read()





share|improve this answer























  • 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













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()





share|improve this answer














You can do this as follows:



with open("foo","r") as f:
string = f.read()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 9:29









Aran-Fey

20.5k53266




20.5k53266










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


















  • 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


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














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















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini