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
python
New contributor
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.
add a comment |
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
python
New contributor
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
Usefile.read()
to get the file contents, and append that to yourall_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
add a comment |
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
python
New contributor
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
python
New contributor
New contributor
edited 2 hours ago
martineau
64.4k887170
64.4k887170
New contributor
asked 2 hours ago
jane
11
11
New contributor
New contributor
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
Usefile.read()
to get the file contents, and append that to yourall_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
add a comment |
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
Usefile.read()
to get the file contents, and append that to yourall_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
add a comment |
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())
add a comment |
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)
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
add a comment |
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
New contributor
Sorry for misreading the question before.
– hhaefliger
2 hours ago
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
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())
add a comment |
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())
add a comment |
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())
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())
answered 2 hours ago
David Barishev
319315
319315
add a comment |
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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
New contributor
Sorry for misreading the question before.
– hhaefliger
2 hours ago
add a comment |
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
New contributor
Sorry for misreading the question before.
– hhaefliger
2 hours ago
add a comment |
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
New contributor
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
New contributor
New contributor
answered 2 hours ago
hhaefliger
28311
28311
New contributor
New contributor
Sorry for misreading the question before.
– hhaefliger
2 hours ago
add a comment |
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
add a comment |
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 yourall_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