How to load and read .txt files as a list instead of individual string variables? [on hold]











up vote
-3
down vote

favorite












I have a folder of .txt files that I need to work with in python while maintaining each files contents as separate from the others.



so instead of



text_1 = "The text of one file"


i want to have



all_text= ["The text of one file", "The text of one file", "The text of one file"]


the files are loading in the order I want them, I just don't know how to load them as individual from each other










share|improve this question









New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as too broad by Marcin, Patrick Artner, martineau, Prune, petezurich 1 hour ago


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
    – Marcin
    2 hours ago










  • Use file.read() to get the file contents, and append that to your all_text list.
    – John Gordon
    2 hours ago






  • 1




    If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
    – Prune
    2 hours ago















up vote
-3
down vote

favorite












I have a folder of .txt files that I need to work with in python while maintaining each files contents as separate from the others.



so instead of



text_1 = "The text of one file"


i want to have



all_text= ["The text of one file", "The text of one file", "The text of one file"]


the files are loading in the order I want them, I just don't know how to load them as individual from each other










share|improve this question









New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as too broad by Marcin, Patrick Artner, martineau, Prune, petezurich 1 hour ago


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
    – Marcin
    2 hours ago










  • Use file.read() to get the file contents, and append that to your all_text list.
    – John Gordon
    2 hours ago






  • 1




    If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
    – Prune
    2 hours ago













up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I have a folder of .txt files that I need to work with in python while maintaining each files contents as separate from the others.



so instead of



text_1 = "The text of one file"


i want to have



all_text= ["The text of one file", "The text of one file", "The text of one file"]


the files are loading in the order I want them, I just don't know how to load them as individual from each other










share|improve this question









New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a folder of .txt files that I need to work with in python while maintaining each files contents as separate from the others.



so instead of



text_1 = "The text of one file"


i want to have



all_text= ["The text of one file", "The text of one file", "The text of one file"]


the files are loading in the order I want them, I just don't know how to load them as individual from each other







python






share|improve this question









New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









martineau

64.4k887170




64.4k887170






New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









jane

11




11




New contributor




jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






jane is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as too broad by Marcin, Patrick Artner, martineau, Prune, petezurich 1 hour ago


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as too broad by Marcin, Patrick Artner, martineau, Prune, petezurich 1 hour ago


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
    – Marcin
    2 hours ago










  • Use file.read() to get the file contents, and append that to your all_text list.
    – John Gordon
    2 hours ago






  • 1




    If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
    – Prune
    2 hours ago














  • 2




    I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
    – Marcin
    2 hours ago










  • Use file.read() to get the file contents, and append that to your all_text list.
    – John Gordon
    2 hours ago






  • 1




    If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
    – Prune
    2 hours ago








2




2




I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
– Marcin
2 hours ago




I'm voting to close this question as off-topic because no effort has been put in by OP to solve this themselves.
– Marcin
2 hours ago












Use file.read() to get the file contents, and append that to your all_text list.
– John Gordon
2 hours ago




Use file.read() to get the file contents, and append that to your all_text list.
– John Gordon
2 hours ago




1




1




If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
– Prune
2 hours ago




If you Google the phrase "Python file input", you’ll find tutorials that can explain it much better than we can in an answer here. StackOverflow is not a coding or tutorial resource.
– Prune
2 hours ago












3 Answers
3






active

oldest

votes

















up vote
2
down vote













Assuming you have a list of files you want to read, this should do it



all_text = 
for f_name in files_to_read:
with open(f_name , 'r') as f:
all_text.append(f.read())





share|improve this answer




























    up vote
    2
    down vote













    Try this



    import os
    dir = YOUR_DIR
    files = os.listdir(dir)
    all_text =
    for file in files:
    with open(dir+file) as file_object:
    contents = file_object.read()
    all_text.append(contents)





    share|improve this answer





















    • Nice adding the directory list from os
      – hhaefliger
      2 hours ago










    • all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
      – roeen30
      2 hours ago


















    up vote
    0
    down vote













    use a for loop:



    files = [list of files]
    files_data =
    for file in files:
    with open(file, 'r') as f:
    files_data.append(f.read())


    this will return an array in which each file is a separate element.



    hope this helps






    share|improve this answer








    New contributor




    hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Sorry for misreading the question before.
      – hhaefliger
      2 hours ago


















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Assuming you have a list of files you want to read, this should do it



    all_text = 
    for f_name in files_to_read:
    with open(f_name , 'r') as f:
    all_text.append(f.read())





    share|improve this answer

























      up vote
      2
      down vote













      Assuming you have a list of files you want to read, this should do it



      all_text = 
      for f_name in files_to_read:
      with open(f_name , 'r') as f:
      all_text.append(f.read())





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        Assuming you have a list of files you want to read, this should do it



        all_text = 
        for f_name in files_to_read:
        with open(f_name , 'r') as f:
        all_text.append(f.read())





        share|improve this answer












        Assuming you have a list of files you want to read, this should do it



        all_text = 
        for f_name in files_to_read:
        with open(f_name , 'r') as f:
        all_text.append(f.read())






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        David Barishev

        319315




        319315
























            up vote
            2
            down vote













            Try this



            import os
            dir = YOUR_DIR
            files = os.listdir(dir)
            all_text =
            for file in files:
            with open(dir+file) as file_object:
            contents = file_object.read()
            all_text.append(contents)





            share|improve this answer





















            • Nice adding the directory list from os
              – hhaefliger
              2 hours ago










            • all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
              – roeen30
              2 hours ago















            up vote
            2
            down vote













            Try this



            import os
            dir = YOUR_DIR
            files = os.listdir(dir)
            all_text =
            for file in files:
            with open(dir+file) as file_object:
            contents = file_object.read()
            all_text.append(contents)





            share|improve this answer





















            • Nice adding the directory list from os
              – hhaefliger
              2 hours ago










            • all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
              – roeen30
              2 hours ago













            up vote
            2
            down vote










            up vote
            2
            down vote









            Try this



            import os
            dir = YOUR_DIR
            files = os.listdir(dir)
            all_text =
            for file in files:
            with open(dir+file) as file_object:
            contents = file_object.read()
            all_text.append(contents)





            share|improve this answer












            Try this



            import os
            dir = YOUR_DIR
            files = os.listdir(dir)
            all_text =
            for file in files:
            with open(dir+file) as file_object:
            contents = file_object.read()
            all_text.append(contents)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            Guilherme Costa

            214




            214












            • Nice adding the directory list from os
              – hhaefliger
              2 hours ago










            • all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
              – roeen30
              2 hours ago


















            • Nice adding the directory list from os
              – hhaefliger
              2 hours ago










            • all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
              – roeen30
              2 hours ago
















            Nice adding the directory list from os
            – hhaefliger
            2 hours ago




            Nice adding the directory list from os
            – hhaefliger
            2 hours ago












            all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
            – roeen30
            2 hours ago




            all_text = [path.read_text() for path in pathlib.Path(YOUR_DIR).iterdir()]
            – roeen30
            2 hours ago










            up vote
            0
            down vote













            use a for loop:



            files = [list of files]
            files_data =
            for file in files:
            with open(file, 'r') as f:
            files_data.append(f.read())


            this will return an array in which each file is a separate element.



            hope this helps






            share|improve this answer








            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Sorry for misreading the question before.
              – hhaefliger
              2 hours ago















            up vote
            0
            down vote













            use a for loop:



            files = [list of files]
            files_data =
            for file in files:
            with open(file, 'r') as f:
            files_data.append(f.read())


            this will return an array in which each file is a separate element.



            hope this helps






            share|improve this answer








            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Sorry for misreading the question before.
              – hhaefliger
              2 hours ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            use a for loop:



            files = [list of files]
            files_data =
            for file in files:
            with open(file, 'r') as f:
            files_data.append(f.read())


            this will return an array in which each file is a separate element.



            hope this helps






            share|improve this answer








            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            use a for loop:



            files = [list of files]
            files_data =
            for file in files:
            with open(file, 'r') as f:
            files_data.append(f.read())


            this will return an array in which each file is a separate element.



            hope this helps







            share|improve this answer








            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 2 hours ago









            hhaefliger

            28311




            28311




            New contributor




            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            hhaefliger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • Sorry for misreading the question before.
              – hhaefliger
              2 hours ago


















            • Sorry for misreading the question before.
              – hhaefliger
              2 hours ago
















            Sorry for misreading the question before.
            – hhaefliger
            2 hours ago




            Sorry for misreading the question before.
            – hhaefliger
            2 hours ago



            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy